我在返回单个数组的类中有一个方法。此方法在同一类内的其他方法中调用。在每个方法的开始时不是定义$data
,而是在扩展类的开始处定义它吗?下面是我想要实现[简化]PHP - 在所有使用方法的类的开头预定义一个数组
class Myclass extends AnotherClass
{
protected $data = $this->getData(); // this does not wwork
public function aMethod()
{
$data = $this->getData();
$data['userName'];
// code here that uses $data array()
}
public function aMethod1()
{
$data = $this->getData();
// code here that uses $data array()
}
public function aMethod2()
{
$data = $this->getData();
// code here that uses $data array()
}
public function aMethod2()
{
$data = $_POST;
// code here that processes the $data
}
// more methods
}
你可以将它设置在__construct()函数,那么它可以用于所有的方法。 – Erik 2014-09-19 09:45:01