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 ?

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

3 个解决方案

#1


29

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类中的所有公共或受保护的属性和方法都将被Child类看作,就像它们是在Child类中定义的那样 - 而另一种方式是arround。

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.

当Child类扩展Parent类时,它可以被视为“Child is a 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 $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 ;-)

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

更多相关文章

  1. ThinkPHP3.2学习——路由_路由定义
  2. 关于静态方法不能调用类中的非静态属性的理解
  3. 我无法定义我的错误
  4. 为什么我的变量在TCPDF中没有定义?
  5. 通过PHP设置html属性的最佳做法是什么?
  6. php预定义常量目录分隔符
  7. 如何使用基于条件的PHP来改变CSS属性?
  8. PHP OOP和MySQLi连接=致命错误:调用未定义的方法sqmyli::arrayQu
  9. MySQL 自定义函数.txt

随机推荐

  1. android 巧用动画使您app风骚起来
  2. Android笔试总结
  3. Android(安卓)MPAndroidChart:动态添加统
  4. 初学者---Android Fragment之间数据传递
  5. android中下载文件到sdcard和进度条小结
  6. 简单明了的分析Android触摸事件,看完再也
  7. Android实例剖析笔记(二)
  8. Android activity 详解一:activity的生命
  9. 一个android工程的运行过程
  10. adb下的tcpdump抓包方法