【发布时间】:2018-06-14 01:40:11
【问题描述】:
我有两个关于 OOP 继承对象的问题:
-
第一个是根据下面的代码:
class Test { private $name = "Youhana"; function getPrivate(){ echo $this->name ; } } Class Test2 extends Test { } $obj2 = new Test2(); $obj2->getPrivate();
结果将是 => Youhana,但是这是如何工作的,尽管继承意味着成员是从父级克隆到子级的,所以如果这是正确的,那么子类中的代码必须类似于以下逻辑:
Class Test2 {// simple imagination to the Test2 after extends Test CLass
function getPrivate(){ //which must return null not the value of the private member.
echo $this->name ;
}
}
参考:Manual
-
第二个问题是考虑下面的代码:
Class Ex1{ function checkClassName(){ var_dump(__CLASS__); } } Class Ex2 extends Ex1{ } $obj2 = new Ex2(); $obj2->checkClassName();//how this will return EX1 although we invoked this function from the second object which after the inheritance will have a clone of public and protected members of the parent class?
虽然我们从第二个对象调用了这个函数,但它如何返回 EX1,在继承之后将有父类的公共和受保护成员的克隆?
【问题讨论】:
-
评论不用于扩展讨论;这个对话是moved to chat。
标签: php oop inheritance