<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="it">
	<id>https://wiki.minerva-apps.com/index.php?action=history&amp;feed=atom&amp;title=Il_modello_Singleton_in_PHP</id>
	<title>Il modello Singleton in PHP - Cronologia</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.minerva-apps.com/index.php?action=history&amp;feed=atom&amp;title=Il_modello_Singleton_in_PHP"/>
	<link rel="alternate" type="text/html" href="https://wiki.minerva-apps.com/index.php?title=Il_modello_Singleton_in_PHP&amp;action=history"/>
	<updated>2026-05-01T15:52:08Z</updated>
	<subtitle>Cronologia della pagina su questo sito</subtitle>
	<generator>MediaWiki 1.31.0</generator>
	<entry>
		<id>https://wiki.minerva-apps.com/index.php?title=Il_modello_Singleton_in_PHP&amp;diff=1254&amp;oldid=prev</id>
		<title>Andrea: Creata pagina con &quot;&amp;larr; ritorno a PHP Category:PHP  fonte: https://phpenthusiast.com/blog/the-singleton-design-pattern-in-php  == The singleton pattern::the good, the bad,...&quot;</title>
		<link rel="alternate" type="text/html" href="https://wiki.minerva-apps.com/index.php?title=Il_modello_Singleton_in_PHP&amp;diff=1254&amp;oldid=prev"/>
		<updated>2023-11-15T08:02:16Z</updated>

		<summary type="html">&lt;p&gt;Creata pagina con &amp;quot;&lt;a href=&quot;/index.php?title=GENERALE#PHP&quot; title=&quot;GENERALE&quot;&gt;← ritorno a PHP&lt;/a&gt; &lt;a href=&quot;/index.php?title=Categoria:PHP&amp;amp;action=edit&amp;amp;redlink=1&quot; class=&quot;new&quot; title=&quot;Categoria:PHP (la pagina non esiste)&quot;&gt;Category:PHP&lt;/a&gt;  fonte: https://phpenthusiast.com/blog/the-singleton-design-pattern-in-php  == The singleton pattern::the good, the bad,...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Nuova pagina&lt;/b&gt;&lt;/p&gt;&lt;div&gt;[[GENERALE#PHP|&amp;amp;larr; ritorno a PHP]] [[Category:PHP]]&lt;br /&gt;
&lt;br /&gt;
fonte: https://phpenthusiast.com/blog/the-singleton-design-pattern-in-php&lt;br /&gt;
&lt;br /&gt;
== The singleton pattern::the good, the bad, and the ugly ==&lt;br /&gt;
Published July 05, 2015&lt;br /&gt;
&lt;br /&gt;
We use the singleton pattern in order to restrict the number of instances that can be created from a resource consuming class to only one.&lt;br /&gt;
&lt;br /&gt;
Resource consuming classes are classes that might slow down our website or cost money. For example:&lt;br /&gt;
* Some external service providers (APIs) charge money per each use.&lt;br /&gt;
* Some classes that detect mobile devices might slow down our website.&lt;br /&gt;
* Establishing a connection with a database is time consuming and slows down our app.&lt;br /&gt;
So, in all of these cases, it is a good idea to restrict the number of objects that we create from the expensive class to only one.&lt;br /&gt;
&lt;br /&gt;
== The anatomy of a singleton pattern ==&lt;br /&gt;
Let's start by understanding the structural characteristics of a class that obeys the singleton pattern:&lt;br /&gt;
&lt;br /&gt;
# A private constructor is used to prevent the direct creation of objects from the class.&lt;br /&gt;
# The expensive process is performed within the private constructor.&lt;br /&gt;
# The only way to create an instance from the class is by using a static method that creates the object only if it wasn't already created&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// General singleton class.&lt;br /&gt;
class Singleton {&lt;br /&gt;
  // Hold the class instance.&lt;br /&gt;
  private static $instance = null;&lt;br /&gt;
  &lt;br /&gt;
  // The constructor is private&lt;br /&gt;
  // to prevent initiation with outer code.&lt;br /&gt;
  private function __construct()&lt;br /&gt;
  {&lt;br /&gt;
    // The expensive process (e.g.,db connection) goes here.&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  // The object is created from within the class itself&lt;br /&gt;
  // only if the class has no instance.&lt;br /&gt;
  public static function getInstance()&lt;br /&gt;
  {&lt;br /&gt;
    if (self::$instance == null)&lt;br /&gt;
    {&lt;br /&gt;
      self::$instance = new Singleton();&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
    return self::$instance;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Why is it a singleton? ==&lt;br /&gt;
Since we restrict the number of objects that can be created from a class to only one, we end up with all the variables pointing to the same, single object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// All the variables point to the same object.&lt;br /&gt;
$object1 = Singleton::getInstance();&lt;br /&gt;
$object2 = Singleton::getInstance();&lt;br /&gt;
$object3 = Singleton::getInstance();&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
[[Image:The-singleton-pattern-explained.png|frame|center|800px]]&lt;br /&gt;
&lt;br /&gt;
== Practical example::database class ==&lt;br /&gt;
Let's demonstrate the singleton pattern with a class that establishes a database connection, and restricts the number of instances to only one.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Singleton to connect db.&lt;br /&gt;
class ConnectDb {&lt;br /&gt;
  // Hold the class instance.&lt;br /&gt;
  private static $instance = null;&lt;br /&gt;
  private $conn;&lt;br /&gt;
  &lt;br /&gt;
  private $host = 'localhost';&lt;br /&gt;
  private $user = 'db user-name';&lt;br /&gt;
  private $pass = 'db password';&lt;br /&gt;
  private $name = 'db name';&lt;br /&gt;
   &lt;br /&gt;
  // The db connection is established in the private constructor.&lt;br /&gt;
  private function __construct()&lt;br /&gt;
  {&lt;br /&gt;
    $this-&amp;gt;conn = new PDO(&amp;quot;mysql:host={$this-&amp;gt;host};&lt;br /&gt;
    dbname={$this-&amp;gt;name}&amp;quot;, $this-&amp;gt;user,$this-&amp;gt;pass,&lt;br /&gt;
    array(PDO::MYSQL_ATTR_INIT_COMMAND =&amp;gt; &amp;quot;SET NAMES 'utf8'&amp;quot;));&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  public static function getInstance()&lt;br /&gt;
  {&lt;br /&gt;
    if(!self::$instance)&lt;br /&gt;
    {&lt;br /&gt;
      self::$instance = new ConnectDb();&lt;br /&gt;
    }&lt;br /&gt;
   &lt;br /&gt;
    return self::$instance;&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  public function getConnection()&lt;br /&gt;
  {&lt;br /&gt;
    return $this-&amp;gt;conn;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since we use a class that checks if a connection already exists before it establishes a new one, it really doesn't matter how many times we create a new object out of the class, we still get the same connection. To prove the point, let's create three instances out of the class and var dump them.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$instance = ConnectDb::getInstance();&lt;br /&gt;
$conn = $instance-&amp;gt;getConnection();&lt;br /&gt;
var_dump($conn);&lt;br /&gt;
&lt;br /&gt;
$instance = ConnectDb::getInstance();&lt;br /&gt;
$conn = $instance-&amp;gt;getConnection();&lt;br /&gt;
var_dump($conn);&lt;br /&gt;
&lt;br /&gt;
$instance = ConnectDb::getInstance();&lt;br /&gt;
$conn = $instance-&amp;gt;getConnection();&lt;br /&gt;
var_dump($conn);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The result is the same connection for the three instances.&lt;br /&gt;
&lt;br /&gt;
== Class that doesn't use a singleton to contact the database ==&lt;br /&gt;
To understand the problem that the singleton pattern solves, let's consider the following class that has no mechanism to check if a connection already exists before it establishes a new connection.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Connect db without a singleton.&lt;br /&gt;
class ConnectDbWOSingleton {&lt;br /&gt;
  private $conn;&lt;br /&gt;
  &lt;br /&gt;
  private $host = 'localhost';&lt;br /&gt;
  private $user = 'db user-name';&lt;br /&gt;
  private $pass = 'db password';&lt;br /&gt;
  private $name = 'db name';&lt;br /&gt;
   &lt;br /&gt;
  // Public constructor.&lt;br /&gt;
  public function __construct()&lt;br /&gt;
  {&lt;br /&gt;
    $this-&amp;gt;conn = new PDO(&amp;quot;mysql:host={$this-&amp;gt;host};&lt;br /&gt;
    dbname={$this-&amp;gt;name}&amp;quot;, $this-&amp;gt;user,$this-&amp;gt;pass,&lt;br /&gt;
    array(PDO::MYSQL_ATTR_INIT_COMMAND =&amp;gt; &amp;quot;SET NAMES 'utf8'&amp;quot;));&lt;br /&gt;
  }&lt;br /&gt;
   &lt;br /&gt;
  public function getConnection()&lt;br /&gt;
  {&lt;br /&gt;
    return $this-&amp;gt;conn;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, each time we create a new object, we also establish a new database connection.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$instance = new ConnectDbWOSingleton();&lt;br /&gt;
$conn = $instance-&amp;gt;getConnection();&lt;br /&gt;
var_dump($conn);&lt;br /&gt;
 &lt;br /&gt;
$instance = new ConnectDbWOSingleton();&lt;br /&gt;
$conn = $instance-&amp;gt;getConnection();&lt;br /&gt;
var_dump($conn);&lt;br /&gt;
 &lt;br /&gt;
$instance = new ConnectDbWOSingleton();&lt;br /&gt;
$conn = $instance-&amp;gt;getConnection();&lt;br /&gt;
var_dump($conn);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This has implications for slowing down the system because each new connection with the database costs time.&lt;br /&gt;
&lt;br /&gt;
== The singleton pattern::the good, the bad, and the ugly ==&lt;br /&gt;
The singleton pattern is probably the most infamous pattern to exist, and is considered an anti-pattern because it creates global variables that can be accessed and changed from anywhere in the code.&lt;br /&gt;
&lt;br /&gt;
Yet, The use of the singleton pattern is justified in those cases where we want to restrict the number of instances that we create from a class in order to save the system resources. Such cases include data base connections as well as external APIs that devour our system resources.&lt;/div&gt;</summary>
		<author><name>Andrea</name></author>
		
	</entry>
</feed>