Question

I have a class UserFavorite that extends User. When i create new object with the class constructor setters cannot set properties.

Constructor:

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); 
    }
}

Setter:

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;
    }

I creatirng new instance $fav = new UserFavorite($user->getUsername(), 1, 'favorite', 'http://abv.bg', 5, 'mamatisiebalo' );

And when i print $fav i receive :

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
)

Any ideas?

Was it helpful?

Solution

You are setting $this->favUrl in the setter function, then overwritting it by assigning the result of the setter function to the same variable.

If you change

$this->favUrl = $this->setFavUrl($favUrl);

To

$this->setFavUrl($favUrl);

You should be OK.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top