我在返回單個數組的類中有一個方法。此方法在同一類內的其他方法中調用。在每個方法的開始時不是定義$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