I have the following script
myclass.php
<?php
$myarray = array('firstval','secondval');
class littleclass {
private $myvalue;
public function __construct() {
$myvalue = "INIT!";
}
public function setvalue() {
$myvalue = $myarray[0]; //ERROR: $myarray does not exist inside the class
}
}
?>
有没有一种简单的声明方式,可以使$myarray在littleclass内可用?我不想将它作为参数传递给构造函数,如果可能的话。
此外,我希望您能够以某种方式使全局变量对php类可见,但这是我第一次面对这个问题,所以我真的不知道。
setvalue()
,取决于你的应用程序中哪种方式更合理,以此避免出现问题。 - kapa