Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如果我有一个配置文件,$var = array('1', '2', '3');如何在类中访问它而不将其定义为常量?var $class_var = $var不起作用。以前,在程序上这是一个问题function this { global $var; echo $var; }。
$var = array('1', '2', '3');
var $class_var = $var
function this { global $var; echo $var; }
有几种方法。您可以将其作为全局对象拉入您的构造中:
class myclass{ var $var; function __construct() { global $var; $this->var = $var; } }
您可以在实例化类时将其作为变量传递:
class myclass{ var $var; function __construct( $var ) { $this->var = $var; } } $class = new myclass( $var );
尽可能不使用全局变量通常是首选。