2011-10-11 95 views
0

我嘗試了所有在網提供的招數,但不知道爲什麼,我不能夠訪問變量..繼承人包含類的私有變量的代碼片段:如何在php中訪問私有變量?

class PANASONIC_PRICESHEET { 

    public $models = array(); 
    public $options = array();  
    public $accessories = array();  

    private $identifier = ''; 
    private $name = ''; 
    private $currency1 = '€'; 
    private $currency2 = '£'; 

    /** 
    * 
    */   
    public function __construct($name1 = 'unnamed', $identifier1 = '') { 
     $this->name = $name1; 
     $this->identifier = $identifier1; 
    } 

    public function getIdentifier() { 
     return $this->identifier;  
    } 

    /** 
    * 
    */   
    public function getName($withIdentifier = false) {  
     if ($withIdentifier) { 
      return $this->name . " - " . $this->identifier; 
     } else { 
      return $this->name; 
     } 
    } 
} 

,這裏是如何我正在訪問它:

$thisName = $pricesheet->getName(); 
$thisIdentifier = $pricesheet->getIdentifier(); 

而且我收到此錯誤:

Fatal error: Cannot access private property PANASONIC_PRICESHEET::$name in 
C:\AppServ\www\dashboard\sites\all\modules\_custom\pricing_system\pricing_system.inc 
on line 316 

如何解決這一問題?我不能讓PUBLIC領域,它根本不是一個選項。任何建議請。

編輯 - 1個

問題已經得到解決: 我是假設調用$ _pricesheet->的getName(); 非常感謝您的建議。

+1

你確定這是所有的代碼,它會產生錯誤? – Shef

+0

什麼@Shef說 - 是真的導致錯誤的代碼? –

+0

Line 316,是你爲了訪問它而放置的兩個之一嗎?或者班上的其中一條線?因爲那裏肯定沒有316線。 – Orbling

回答

0

這是目前爲我工作。看看here。您必須可能試圖訪問$pricesheet->name;

0

您可以嘗試在公用函數中調用私有函數,該函數返回私有變量。 類似於Javascript中的閉包。

希望它的作品