2013-04-25 73 views
0

我試圖獲取類的屬性在PHPPHP - 類財產問題

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); 

我需要調用getFilePathimport方法,但我只能得到$this->fileInfoimport方法。 有沒有反正呢?非常感謝!

+3

'class test(){...}'是不對的。 http://www.php.net/manual/en/language.oop5.basic.php – Ejaz 2013-04-25 20:59:08

+0

對不起,這只是一個快速的錯字。謝謝你的提示。 – Rouge 2013-04-25 21:00:17

+0

是的,你的代碼沒有多大意義。就像,當你創建並填充一個新的ImportData對象時('New ImportData($ this,$ this-> fileInfo)'),但是不要將它分配給任何東西,導致一個對象無法真正引用它。 – 2013-04-25 21:01:04

回答

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 
} 
+1

'class Test(){...'無效的語法:) – Ejaz 2013-04-25 21:01:36

+0

根據您的意見更新 – Dan 2013-04-25 21:02:17