I have next code. Why field userId is invisible in InheritUser?
class User{
private $userId;
function User($userId){
$this->userId = $userId;
}
function getId(){
return $this->userId;
}
}
class InhreritUser extends User{
function someFunc(){
echo $this->userId; // nothing
}
}
someFunc returns nothing:
$inheritUser = new InheritUser(1);
$inheritUser->someFunc();
That's the point of the private
keyword. If you use protected
this will work.
See: http://php.net/language.oop5.visibility
Also, that code would have thrown an error, if you didn't turn off errors in PHP (bad idea during development).
http://php.net/manual/en/language.oop5.visibility.php
A class member needs to be protected for it to be visible to a subclass. Private means that subclasses won't be able to see it.
protected $userId;
It's private. Make it protected instead.
Private fields are accessible to the class only. Protected fields are available to subclasses too.