Today it looks like the singleton pattern inside the design pattern, the so-called Singleton pattern is that no matter how much this class is instantiated, the entire application only produces an object instance, and all of the objects are simply references to this object instance. Here is the code for PHP to implement a singleton pattern.
<?php
Class Person
{
Private $name;
Private $sex;
Public Function SetName ($name)
{
$this->name= $name;
}
Public Function Setage ($age)
{
$this->age= $age;
}
Public Function Printinfo ()
{
echo $this->name, $this->age;
}
}
Class single
{
private static $_instance = NULL;
Private $person = new person;
public static function getinstance ()
{
if (! $this->_instance instanceof $person) {
Self::$_instance= $person
}
return self::$_instance;
}
}
$p 1 = single::getinstance ();
$p 1->setname ("Chenlei");
$p 1->setage (28);
$p 2 = single::getinstance ();
$p 2->printinfo ();//output should be chenlei28
End.
Get off work, write more hasty, there are mistakes to point out, thank you!
PHP Implementation Singleton mode