note 113689 deleted from language.oop5.static by cmb

From: Date: Sun, 01 Sep 2019 07:33:13 +0000
Subject: note 113689 deleted from language.oop5.static by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-212058@lists.php.net to get a copy of this message
Note Submitter: desmatic ---- Don't use GLOBALS in classes (or anywhere really), use static variables instead. They're better. They can do everything a GLOBAL can and they can be protected by accessor functions so they'll never get clobbered. class foo { private static $_private = null; public function get() { if (self::$_private === null) { self::$_private = new stdClass(); } return self::$_private; } } class bar extends foo { } function scope_foo() { $f = new foo(); $f->get()->name = "superdude"; } function scope_bar() { $b = new bar(); $b->get()->description = "an object reference test"; } scope_foo(); scope_bar(); $my = new bar(); foreach ($my->get() as $key => $value) { echo "{$key} => {$value}\n"; } outputs: name => superdude description => an object reference test

« previous php.notes (#212058) next »