drbz99867 2014-06-05 17:26
浏览 54
已采纳

使用PDO(php)不在子类中映射SuperClass字段

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!

  • 写回答

1条回答 默认 最新

  • dovgqc2648 2014-06-14 13:21
    关注

    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;
        ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果