[英]How to call parent class functions when extending in child class
Below is my sample code given下面是我给出的示例代码
<?php
Class A {
function SelectRecord()
{
$this->DeleteRecord();
echo "class A - SelectRecord ";
}
function DeleteRecord()
{
echo "class A - DeleteRecord ";
}
}
Class B extends a {
function SelectRecord()
{
Parent::SelectRecord();
echo "class B - SelectRecord ";
}
function DeleteRecord()
{
echo "class B - DeleteRecord ";
}
}
$objB = new B();
$objB->SelectRecord();
Output I get is Output 我得到的是
class B - DeleteRecord
class A - SelectRecord
class B - SelectRecord
How can I call the class A DeleteRecord method in class A itself when extending in Class B. When I tried to call from Class A it calls the Class B DeleteRecord method. How can I call the class A DeleteRecord method in class A itself when extending in Class B. When I tried to call from Class A it calls the Class B DeleteRecord method. When I use self::DeleteRecord.
当我使用 self::DeleteRecord。 It works fine.
它工作正常。 But when to $this and Self.
但是什么时候 $this 和 Self. Shall I replace $this to Self wherever it comes?
我应该将 $this 替换为 Self 吗?
You can specify the exact class for the method that is being called:您可以为正在调用的方法指定确切的 class:
<?php
Class A {
function SelectRecord()
{
A::DeleteRecord();
echo "class A - SelectRecord ";
}
function DeleteRecord()
{
echo "class A - DeleteRecord ";
}
}
Class B extends a {
function SelectRecord()
{
Parent::SelectRecord();
echo "class B - SelectRecord ";
}
function DeleteRecord()
{
echo "class B - DeleteRecord ";
}
}
$objB = new B();
$objB->SelectRecord();
results结果
class A - DeleteRecord
class A - SelectRecord
class B - SelectRecord
Explanation and further reading:解释和延伸阅读:
The mechanism that causes the derived class`s method to be invoked is called virtual method .导致调用派生类方法的机制称为虚拟方法。 It is used to facilitate polymorphism .
它用于促进 多态性。 All methods in PHP are virtual by default.
PHP 中的所有方法默认都是虚拟的。 You can prevent method from being overrided marking it by keyword 'final'.
您可以通过关键字“final”来防止方法被覆盖标记它。
As I understand your classes are not trying to solve any practical problem and this is just a language learning exercises.据我了解,你们的课程并没有试图解决任何实际问题,这只是一个语言学习练习。 There is a great guide on using inheritance in Joshua Bloch's Effective Java :
在 Joshua Bloch 的有效 Java中有一个关于使用 inheritance 的很好的指南:
Inheritance is appropriate only in circumstances where the subclass really is a subtype of the superclass.
Inheritance 仅适用于子类确实是超类的子类型的情况。 In other words, a class B should extend a class A only if an “is-a” relationship exists between the two classes.
换句话说,只有当两个类之间存在“is-a”关系时,class B 才应该扩展 class A。 If you are tempted to have a class B extend a class A, ask yourself the question: Is every B really an A?
如果您想拥有一个 class B 扩展一个 class A,问自己一个问题:每个 B 真的都是 A 吗? If you cannot truthfully answer yes to this question, B should not extend A. If the answer is no, it is often the case that B should contain a private instance of A and expose a smaller and simpler API: A is not an essential part of B, merely a detail of its implementation.
如果你不能如实回答这个问题,B不应该扩展A。如果答案是否定的,通常情况下B应该包含A的私有实例并暴露一个更小更简单的API:A不是必不可少的部分B,只是其实现的一个细节。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.