php - Protecting stand-alone variables -
is possible protect defined variable overriding (changing value)? making system uses plugins , want prevent plugin writer (myself) changing of specific defined variables (objects holds new class instances).
something this:
class foo { function __construct() { return "hello"; } } $bar= new foo(); so later, using global $bar; in functions don't want allow changing variable this:
$bar = new foo(); $bar = "new value";
$bar must stay same (new foo()) since going big system , cannot remember hundreds of core variables defined.
ideally be, if try redefine - php should throw fatal error. there such thing?
this sounds singleton pattern:
class foo { protected static $instance = null; protected function __construct() { throw new exception('use ::getinstance()'); } public static function getinstance() { if (!isset(static::$instance)) { static::$instance = new static; } return static::$instance; } } use:
$bar = foo::getinstance(); any 1 can redeclare $bar .. if wont real foo ... need instance;
Comments
Post a Comment