note 49036 deleted from language.oop5.static by cmb

From: Date: Sun, 01 Sep 2019 07:23:12 +0000
Subject: note 49036 deleted from language.oop5.static by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-212048@lists.php.net to get a copy of this message
Note Submitter: c_daught_d at earthlink dot net ---- A twist on christian at koch dot net's Singleton example is setting/getting non-static member variables using self::$instance->varname within static method calls. Within the modified Singleton class below, the member variable $value is set within the getInstance static method instead of the constructor. Whether this is "pure" OPP, I don't know. But it does work, is worth mentioning, and could be usefull. class Singleton { private static $instance=null; private $value=null; private function __construct() { } public static function getInstance() { if ( self::$instance == null ) { echo "<br>new<br>"; self::$instance = new Singleton("values"); self::$instance->value = "values"; } else { echo "<br>old<br>"; } return self::$instance; } }

« previous php.notes (#212048) next »