我有一个扩展用户的UserFavorite类.当我使用类构造函数创建新对象时,setters无法设置属性.
构造函数:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
二传手:
public function setFavUrl($favUrl) {
$url = filter_var($favUrl, FILTER_VALIDATE_URL);
if (!$url) {
echo $this->showError(...);
exit;
}
echo $url; // THIS LOGS THE URL
$this->favUrl = $url;
}
Run Code Online (Sandbox Code Playgroud)
我创造了新的实例 $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
)
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
您正在设置$this->favUrlsetter函数,然后通过将setter函数的结果分配给同一个变量来覆盖它.
如果你改变了
$this->favUrl = $this->setFavUrl($favUrl);
Run Code Online (Sandbox Code Playgroud)
至
$this->setFavUrl($favUrl);
Run Code Online (Sandbox Code Playgroud)
你应该没事.