PHP use variable from global space inside class -
hi i'm writing code in php. need use variable global scope inside class doesn't work. don't know if need use namespace or not , how ? thank sample :
<?php $plantask_global_script = array("one", "two"); $plantask_global_config = array("three", "four"); $myvar = array_merge($plantask_global_script, $plantask_global_config); class env implements arrayaccess { static private $container = $myvar; public function offsetset($offset, $value) { if (is_null($offset)) { self::$container[] = $value; } else { self::$container[$offset] = $value; } } public function offsetexists($offset) { return isset(self::$container[$offset]); } public function offsetunset($offset) { unset(self::$container[$offset]); } public function offsetget($offset) { return isset(self::$container[$offset]) ? self::$container[$offset] : null; } }
try calling $myvar
superglobal:
private static $container = $globals['myvar'];
although, ron dadon pointed out, bad practice in oop.
edit:
i jumped gun here. above solution does not work, @ least not me. so, better way achieve following:
$myvar = array_merge($plantask_global_script, $plantask_global_config); class env implements arrayaccess { private static $container = null; public static function init($var) { self::$container = $var; } ... } env::init($myvar);
Comments
Post a Comment