2011-10-11 34 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中的闭包。

希望它的作品