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.
我在课堂上有以下内容。
class My_Class { $x = 'happy'; $y = array( 'iam' => $this->x); //getting a 500 error with that. function __construct() { // scripts, etc } }
我得到了一个意想不到的 $this (T_VARIABLE)。有什么想法吗?
定义实例变量时不能使用 $this。试试这个替代方案:
class My_Class { var $x = 'happy'; var $y = array(); function __construct() { $this->y['iam'] = $this->x; } }
您不能在类定义中放置变量。常量有效,但其他任何东西(连接等)都会引发错误。