php 从孩子访问父类的属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2283870/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:52:29  来源:igfitidea点击:

Accessing Parent Class' property from child

phpoop

提问by shxfee

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). 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 ?

在这里访问我正在使用该类的实例的父母属性,这似乎是错误的:S(那时不需要成为我的孩子)。有没有一种简单的方法,以便我可以将父属性与子属性同步,并且可以直接访问 $this->anotherP 而不必使用 $this->parent->anotherP ?

回答by Pascal MARTIN

As your Childclass is extending your Parentclass, every properties and methods that are either publicor protectedin the Parentclass will be seen by the Childclass as if they were defined in the Childclass -- and the other way arround.

当你的Child类扩展您的Parent课,每次要么是属性和方法publicprotectedParent类将由被视为Child类,仿佛他们在被定义Child的类-和其他方式角落找寻。

When the Childclass extendsthe Parentclass, it can be seen as "Childis a Parent"-- which means the Childhas the properties of the Parent, unless it redefines those another way.

ChildextendsParent类时,它可以被视为Child是一个Parent——这意味着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 $dataproperty, defined in the MyParentclass, and initialized in a method of that same MyParentclass, is accessible by the Childclass as if it were its own.

这意味着$dataMyParent类中定义并在同一个MyParent类的方法中初始化的属性可以被Child类访问,就好像它是它自己的一样。


To make things simple : as the Child"is a" MyParent, it doesn't need to keep a pointer to... itself ;-)


为了简单起见:作为Child“是一个” MyParent,它不需要保持指向...本身的指针;-)

回答by Robert Sinclair

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...

请记住:您的 Child 类只继承了 Parent 类中定义的属性...因此,如果您使用 Parent 类实例化一个对象,然后用数据填充它,那么该数据在您的子类中将不可用...

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. This way you have access to all the properties and methods of the object generated by parent class

一个超级简单的解决方案是不扩展任何东西,只需通过构造函数将父类的 $object 传递给子类。这样你就可以访问父类生成的对象的所有属性和方法

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;
}