我尝试了所有在网提供的招数,但不知道为什么,我不能够访问变量..继承人包含类的私有变量的代码片段:如何在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(); 非常感谢您的建议。
你确定这是所有的代码,它会产生错误? – Shef
什么@Shef说 - 是真的导致错误的代码? –
Line 316,是你为了访问它而放置的两个之一吗?或者班上的其中一条线?因为那里肯定没有316线。 – Orbling