Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You are creating a "<code>sam</code>" object by "<code>new sam</code>", you sould use "<code>sam::getInstance()</code>"; to reach the static instance but it won't be "sam object" type will be "Singleton" "."<strong>__<em>CLASS</em>__</strong>" gives the scope class not the real object class.</p> <p><strong>First</strong>: you must read about "<a href="http://php.net/manual/en/language.oop5.late-static-bindings.php" rel="nofollow">late static binding</a>" in php and learn the limitations of self:: and <strong>__<em>CLASS</em>__</strong> use "<code>static::</code>" instead of "<code>self::</code>" (5.3+)</p> <p>Or you can change all the pattern use statics like;</p> <pre><code> &lt;?php class Singleton { // A static property to hold the single instance of the class private static $instance; // The constructor is private so that outside code cannot instantiate public function __construct() { } // All code that needs to get and instance of the class should call // this function like so: $db = Database::getInstance(); public static function getInstance() { // If there is no instance, create one if (!isset(self::$instance)) { $c = __CLASS__; self::$instance = new $c; } return self::$instance; } // Block the clone method private function __clone() {} // Function for inserting data to object public function insertData($param, $element) { $this-&gt;{$param} = $element; } } //---CLASS ram--- class ram { function __construct() { $db = Singleton::getInstance(); $db-&gt;insertData('name', 'Suresh'); } } $obj_ram = new ram; //---CLASS sam--- class sam extends Singleton { function __construct() { parent::__construct(); } public static function show_data() { echo "&lt;br&gt;Data in current object&lt;br&gt;"; print_r(self::getInstance()); echo "&lt;br&gt;&lt;br&gt;Data in singleton object&lt;br&gt;"; $singleton_obj = Singleton::getInstance(); print_r($singleton_obj); } } $obj_sam = sam::getInstance(); print_r($obj_sam); echo sam::show_data(); </code></pre> <p>This is an example which setting the properties pointers to the current object like "CI"</p> <pre><code>&lt;?php class Singleton { // A static property to hold the single instance of the class private static $instance; // The constructor is private so that outside code cannot instantiate public function __construct() { if(isset(self::$instance)) foreach(self::$instance as $key =&gt; &amp;$val) { $this-&gt;{$key} = &amp;$val; } } // All code that needs to get and instance of the class should call // this function like so: $db = Database::getInstance(); public static function getInstance() { // If there is no instance, create one if (!isset(self::$instance)) { $c = __CLASS__; self::$instance = new $c; } return self::$instance; } // Block the clone method private function __clone() {} // Function for inserting data to object public function insertData($param, $element) { $this-&gt;{$param} = $element; } } //---CLASS ram--- class ram { function __construct() { $db = Singleton::getInstance(); $db-&gt;insertData('name', 'Suresh'); } } class ram2 { function __construct() { $db = Singleton::getInstance(); $db-&gt;insertData('name', 'Suresh'); $db-&gt;insertData('name2', 'Suresh2'); } } $obj_ram = new ram; $obj_ram = new ram2; //---CLASS sam--- class sam extends Singleton { function __construct() { parent::__construct(); } public function show_data() { echo "&lt;br&gt;Data in current object&lt;br&gt;"; print_r($this); echo "&lt;br&gt;&lt;br&gt;Data in singleton object&lt;br&gt;"; $singleton_obj = Singleton::getInstance(); print_r($singleton_obj); } } $obj_sam = new sam; echo $obj_sam-&gt;show_data(); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload