I have the PersistentObject class as following
abstract class PersistentObject implements IPersistable
{
private $id;
public function getId()
{
return $this->id;
}
public function setId($id)
{
$this->id = $id;
}
}
and the UserModel extending the PersistentObject
class UserModel extends PersistentObject
{
public static $TABLE_NAME = "user";
private $email;
private $username;
private $password;
public function getEmail()
{
return $this->email;
}
public function setEmail($email)
{
$this->email = $email;
}
public function getUsername()
{
return $this->username;
}
public function setUsername($username)
{
$this->username = $username;
}
public function getPassword()
{
return $this->password;
}
public function setPassword($password)
{
$this->password = $password;
}
}
now when I fetch the usermodel with pdo
$entity = $stmt->fetchObject("UserModel");
I'm getting the result ( var_dump($entity) ):
object(UserModel)[11]
private 'email' => string 'andrewwww@gmail.com' (length=24)
private 'username' => string 'andrewww' (length=13)
private 'password' => string '72bed4064dbe53d7fc5fd078214387c813c1f670' (length=40)
private 'id' (PersistentObject) => null
public 'id' => string '2' (length=1)
and if I try
var_dump($entity->getId());
I receive null;
How is it possible to map the superclass fields into the subclass??? thnx!
That's exactly the difference between private
and protected
.
From http://php.net/manual/en/language.oop5.visibility.php:
Members declared protected can be accessed only within the class itself and by inherited and parent classes. Members declared as private may only be accessed by the class that defines the member.
So use protected
in the parent class:
abstract class PersistentObject implements IPersistable
{
protected $id;
...