简体   繁体   English

PHP在父级中访问子级的私有属性

[英]PHP Accessing a child's private properties in parent

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. 我有一个父对象,我在我的应用程序中用于一般CRUD - 它有基本的保存和检索方法,所以我不必将它们重新包含在我的所有对象中。 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. 我在创建子实例的父对象中使用“retrieve”方法,然后从未序列化的子元素的属性中填充自己 - 这意味着可以“自我反序列化”对象。

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. 所以我正在寻找一种更好的方法来“自我反序列化”或者一种允许父对象“看到”受保护属性的方法 - 但只是在检索过程中。

Example of the code: 代码示例:

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. 编辑:应该说“私人”不受保护的儿童财产。

try it like this: 试试这样:

abstract class ParentClass
{
  protected abstract function GetChildProperty();

  public function main()
  {
    $value = $this->GetChildProperty();
  }
}

class ChildClass extends ParentClass
{
  private $priv_prop = "somevalue";

  protected function GetChildProperty()
  {
    return $this->priv_prop;
  }
}

The best possible answer to fix this is to use reflections . 解决此问题的最佳答案是使用反射

Example: 例:

$_SESSION[''] = ''; // init

class Base {
    public function set_proxy(){
        $reflectionClass = new ReflectionClass($this);
        $ar = $reflectionClass->getDefaultProperties();

        !isset($ar['host'])  or  $_SESSION['host'] = $ar['host'];
    }
}

class Son1 extends Base {
    private $host = '2.2.2.2';
}

class Son2 extends Son1 {

}


$son1 = new Son1();
$son1->set_proxy(); 
var_dump($_SESSION); // array(2) { [""]=> string(0) "" ["host"]=> string(7) "2.2.2.2" }

unset($_SESSION);
$_SESSION[''] = ''; // init

$son2 = new Son2();
$son2->set_proxy(); 
var_dump($_SESSION); //  array(1) { [""]=> string(0) "" }

如何在子对象中返回$ this-> unretrievableProperty的getProperty()函数

It doesn't seem that same class visibility policy applies to iherited/parent classes. 似乎相同的类可见性策略不适用于iherited / parent类。 The php documentation does not address this. php文档没有解决这个问题。

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. 我建议您将retrieve方法声明为static,并通过静态调用而不是当前的“self unserialize”方法获取$ cleanTester。

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

[...]

$cleanTester = BaseObject::retrieve();

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. 或者您可以使用__get()方法来访问不可访问的属性...我相信这可以添加到BaseObject类并从子类中获取受保护的属性。 Since the same class visibility policy should apply to BaseObject you could define the __get() method private or protected. 由于相同的类可见性策略应该应用于BaseObject您可以将__get()方法定义为private或protected。

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

    return null;
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM