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
privatekeyword. If you useprotectedthis 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).