简体   繁体   English

从孩子访问父类的属性

[英]Accessing Parent Class' property from child

See the following example (PHP) 请参见以下示例(PHP)

class Parent
{ 
  protected $_property;
  protected $_anotherP;

  public function __construct($var)
  {
    $this->_property = $var;
    $this->someMethod();  #Sets $_anotherP
  }

  protected function someMethod()
  ...
}

class Child extends Parent
{
  protected $parent;

  public function __construct($parent)
  {
    $this->parent = $parent;
  }

  private function myMethod()
  {
    return $this->parent->_anotherP;  #Note this line
  }
}

I am new to OOP and am a bit ignorant. 我是OOP的新手,有点无知。

Here to access the parents property I am using an instance of that class, which seems wrong :S (no need of being i child then). 在这里,我使用的是该类的实例来访问parents属性,这似乎是错误的:S(然后不需要成为i子代)。 Is there an easy way, so that i can sync the parent properties with the child properties and can directly access $this->anotherP without having to use $this->parent->anotherP ? 有没有一种简单的方法,这样我就可以将父属性与子属性同步并可以直接访问$ this-> anotherP,而不必使用$ this-> parent-> anotherP?

As your Child class is extending your Parent class, every properties and methods that are either public or protected in the Parent class will be seen by the Child class as if they were defined in the Child class -- and the other way arround. 当您的Child类扩展您的Parent类时, Child类将看到在Parent类中publicprotected所有属性和方法,就像它们是在Child类中定义的一样;反之亦然。

When the Child class extends the Parent class, it can be seen as " Child is a Parent " -- which means the Child has the properties of the Parent , unless it redefines those another way. Childextends Parent类时,可以将其视为ChildParent -这意味着Child具有Parent的属性,除非以其他方式重新定义了这些属性。

(BTW, note that " parent " is a reserved keyword, in PHP -- which means you can't name a class with that name) (顺便说一句,注意“ parent ”是PHP中的保留关键字,这意味着您不能使用该名称来命名类)


Here's a quick example of a "parent" class : 这是“父”类的简单示例:

class MyParent {
    protected $data;
    public function __construct() {
        $this->someMethodInTheParentClass();
    }
    protected function someMethodInTheParentClass() {
        $this->data = 123456;
    }
}

And it's "child" class : 这是“孩子”类:

class Child extends MyParent {
    public function __construct() {
        parent::__construct();
    }
    public function getData() {
        return $this->data; // will return the $data property 
                            // that's defined in the MyParent class
    }
}

That can be used this way : 可以这样使用:

$a = new Child();
var_dump($a->getData());

And you'll get as output : 然后您将得到输出:

int 123456

Which means the $data property, defined in the MyParent class, and initialized in a method of that same MyParent class, is accessible by the Child class as if it were its own. 这意味着在MyParent类中定义并在相同MyParent类的方法中初始化的$data属性可以由Child类访问,就好像它是自己的一样。


To make things simple : as the Child "is a" MyParent , it doesn't need to keep a pointer to... itself ;-) 为了简单MyParent :由于Child “是” MyParent ,它不需要保持指向...本身的指针;-)

This may save you a few hours of searching around. 这样可以节省您几个小时的搜索时间。

Remember: Your Child class only inherits the properties DEFINED in the Parent class... So if you instantiate an object using Parent class and then populate it with data, then this data will NOT be available in your child class... 请记住:您的子类仅继承父类中定义的属性...因此,如果您使用父类实例化一个对象,然后用数据填充该对象,那么该数据将在您的子类中不可用...

It's super obvious of course, but I'm guessing others may run into the same issue. 这当然是非常明显的,但是我猜想其他人可能会遇到同样的问题。

A super simple solution is not to extend anything, simply pass the $object of your parent class into your child class through a constructor. 一个超级简单的解决方案是不扩展任何内容,只需通过构造函数将父类的$ object传递给子类。 This way you have access to all the properties and methods of the object generated by parent class 这样,您可以访问父类生成的对象的所有属性和方法

Example

class child {

    public parentObject;

    public function __construct($parentObject) {
        $this->parentObject = $parentObject;
    }

}

If your $parentObject has a public property $name, then you can access it inside the child class with a function like: 如果您的$ parentObject具有公共属性$ name,则可以使用以下函数在子类内部访问它:

public function print_name() {
    echo $this->parentObject->name;
}

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

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