PHP在父级中访问子级的私有属性 [英] PHP Accessing a child's private properties in parent

查看:106
本文介绍了PHP在父级中访问子级的私有属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在应用程序中有一个用于通用CRUD的父对象-它具有基本的save&检索方法,因此我不必将它们重新包含在我的所有对象中.我的大多数子对象都扩展了此基础对象.这个工作正常,但是我在检索序列化的子对象时发现了一个问题.我在父对象中使用检索"方法,该方法创建子对象的实例,然后从未序列化的子对象的属性填充自身-这意味着可以自我反序列化"对象.

I have a parent object that I use for general CRUD in my applications - it has basic save & retrieve methods so I can don't have to reinclude them them in all my objects. Most of my child objects extend this base object. This has worked fine, but I'm finding a problem with retrieving a serialized child object. I use a "retrieve" method in the parent object that creates an instance of the child, then populates itself from the properties of the unserialized child - this means is can "self unserialize" the object.

唯一的问题是-如果子对象具有受保护或私有属性,则父对象无法读取它,因此在检索过程中不会将其拾取.

Only problem is - if the child object has a protected or private property, the parent object can't read it, so it doesn't get picked up during retrieval.

因此,我正在寻找一种更好的方法来自我反序列化"或允许父对象查看"受保护的属性-但仅限于在检索过程中.

So I'm looking either for a better way to "self unserialize" or a way to allow a parent object to "see" the protected properties - but only during the retrieval process.

代码示例:

BaseObject {

 protected $someparentProperty;

 public function retrieve() {

  $serialized = file_get_contents(SOME_FILENAME);
  $temp = unserialize($serialized);
  foreach($temp as $propertyName => $propertyValue) {
    $this->$propertyName = $propertyValue;
  }     

 }

 public function save() {

    file_put_contents(SOME_FILENAME, serialize($this));
 }
}

class ChildObject extends BaseObject {

 private $unretrievableProperty;  

 public setProp($val) {
    $this->unretrivableProperty = $val;
 }
}

$tester = new ChildObject();
$tester->setProp("test");
$tester->save();

$cleanTester = new ChildObject();
$cleanTester->retrieve();
// $cleanTester->unretrievableProperty will not be set

应该说私人"未受保护的子财产.

EDITED: Should have said "Private" not protected child properties.

推荐答案

似乎相同的类可见性策略不适用于继承的/父类. php文档未解决此问题.

It doesn't seem that same class visibility policy applies to iherited/parent classes. The php documentation does not address this.

我建议您声明检索方法为静态,并通过静态调用而不是当前的自我反序列化"方法来获取$ cleanTester.

I would suggest that you declared the retrieve method static, and fetched the $cleanTester through a static call rather than your current "self unserialize" approach.

static function retrieve() {
  $serialized = file_get_contents(SOME_FILENAME);
  return unserialize($serialized);
}

[...]

$cleanTester = BaseObject::retrieve();

或者您可以利用__get()方法访问不可访问的属性...我相信可以将其添加到BaseObject类并从子类中获取受保护的属性.由于相同的类可见性策略应应用于BaseObject,因此您可以将__get()方法定义为私有或受保护.

Or you could utilize the __get() method to access inaccessible properties... I believe this could be added to the BaseObject class and fetch protected properties from the child class. Since the same class visibility policy should apply to BaseObject you could define the __get() method private or protected.

BaseObject {
  private function __get($propertyName) {
    if(property_exists($this,$propertyName))
      return $this->{$propertyName};

    return null;
  }

这篇关于PHP在父级中访问子级的私有属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆