PHP Classes

avoid misunderstanding about singleton

Recommend this page to a friend!

      Singleton Class  >  All threads  >  avoid misunderstanding about singleton  >  (Un) Subscribe thread alerts  
Subject:avoid misunderstanding about singleton
Summary:I can get new instances of class
Messages:6
Author:adriano ghezzi
Date:2017-07-22 04:34:01
 

  1. avoid misunderstanding about singleton   Reply   Report abuse  
Picture of adriano ghezzi adriano ghezzi - 2017-07-22 04:34:01
The singleton pattern should be used to avoid new instances of a class

for this reason the pattern is applied to the class itself

in your example if I do

$x=new test();
I'll get a new instance of test


  2. Re: avoid misunderstanding about singleton   Reply   Report abuse  
Picture of Mohamed Elbahja 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())


















  3. Re: avoid misunderstanding about singleton   Reply   Report abuse  
Picture of adriano ghezzi adriano ghezzi - 2017-07-22 11:04:29 - In reply to message 2 from Mohamed Elbahja
hello

for all the classes you want to get an instance you must set the constructor as private

so developer can get a new instance with c =new A();

normally is

class A {

private static $_me;

private function __construct(){
//nothing here
}

public static function factory(){
if(!isset(self::$_me))
self::$_me = new A();
return self::$_me;
}
}

//so far
$a=A::factory();
var_dump($a);//object(A)[1]

$b=A::factory();//object(A)[1]
var_dump($b);//object(A)[1]

//but you can not

$c=new A();
//Fatal error: Call to private A::__construct() from invalid context i








  4. Re: avoid misunderstanding about singleton   Reply   Report abuse  
Picture of Mohamed Elbahja Mohamed Elbahja - 2017-07-22 13:42:02 - In reply to message 3 from adriano ghezzi
hi,
you was right
i will update class
thanks :)

  5. Re: avoid misunderstanding about singleton   Reply   Report abuse  
Picture of Mohamed Elbahja Mohamed Elbahja - 2017-07-22 17:36:43 - In reply to message 3 from adriano ghezzi
has been updated thanks for info

  6. Re: avoid misunderstanding about singleton   Reply   Report abuse  
Picture of adriano ghezzi adriano ghezzi - 2017-07-22 17:56:46 - In reply to message 5 from Mohamed Elbahja
ok now well done!