1
我有一个UserFavorite类扩展了用户。 当我用类构造函数创建新对象setter无法设置属性。PHP OOP无法设置私有属性
构造:
public function __construct($username, $tabId, $favName, $favUrl = null, $favPosition = null, $favComment = null) {
parent::__construct($username);
$this->tabId = $this->setTabId($tabId);
$this->favName = $this->setFavName($favName);
$this->favUrl = $this->setFavUrl($favUrl);
$this->favPosition = $this->setFavPosition($favPosition);
if ($favComment) {
$this->favComment = $this->setFavComment($favComment);
}
}
二传手:
我creatirng新实例$fav = new UserFavorite($user->getUsername(), 1, 'favorite', 'http://abv.bg', 5, 'mamatisiebalo');
当我打印$fav
我收到:
favorite<pre>UserFavorite Object
(
[favName:UserFavorite:private] =>
[tabId:UserFavorite:private] =>
[favUrl:UserFavorite:private] =>
[favPosition:UserFavorite:private] =>
[favComment:UserFavorite:private] =>
[_favId:UserFavorite:private] =>
[username:protected] => myUserName
[_userId:protected] => 1
)
任何想法?
如果这些属性是'User'类中的私有属性,那么您无法从'UserFavourites'类中访问它们....这就是'private'意味着什么 –
'$ this-> tabId = $ this- > setTabId($ tabId);'你为什么手动设置它并调用setter?它应该是'$ this-> setTabId($ tabId);'或'$ this-> tabId = $ tabId;' – Jessica
+1 Mark Baker所说的。如果你正在扩展这个类,并且想要访问父变量,使用'protected'而不是'private'。 – Crackertastic