我试图在PHP中获取类的属性
class Test(){
public function import($data) {
$im = new import($this, $data);
$im->getInfo();
$this->fileInfo = $im->getRecord();
new ImportData($this, $this->fileInfo);
return true;
}
public function getFilePath(){
return $this->fileInfo;
}
}
在我的其他档案中。
$import = new Test();
$filePath = $import ->getFilePath();
//$filePath returns nothing becasue $this->fileInfo is not init yet.
//my other codes here.
//my other codes here.
//call import method here.
$import ->import($data);
我需要在getFilePath
方法之前致电import
,但我只能在$this->fileInfo
方法中获取import
。
这周围有什么吗?非常感谢!
答案 0 :(得分:1)
许多例子中的一个......在你的类中使用你的构造方法初始化你需要在类范围内的变量和方法。
class TestClass
{
private $variable // should initialized in construct to be available via $this->variable class wide
public function __construct($data)
{
// initialize methods and code here so they will be available throughout the calss
$this->variable = $this->getinfo($data); // this will be initialized when you instantiate your class object.
}
//etc methods
}