
Mohamed Elbahja - 2017-07-22 10:23:40 -
In reply to message 1 from adriano ghezzi
hi,
the idea is:
class A
{
private static $instance;
public function __construct()
{
//
}
public static function getInstance()
{
if (static::$instance === null) {
static::$instance = new static();
}
return static::$instance;
}
}
class B
{
private static $instance;
public function __construct()
{
//
}
public static function getInstance()
{
if (static::$instance === null) {
static::$instance = new static();
}
return static::$instance;
}
}
$A = A::getInstance();
using Singleton Class:
class A
{
public function __construct()
{
//
}
}
class B
{
public function __construct()
{
//
}
}
// autoload via composer
// "autoload": {
// "files": ["path/to/singletonClass.php"]
// }
// get instance
$A = Singleton::getInstance('A');
$B = Singleton::getInstance('B');
var_dump(Singleton::getInstances())