首页  >  文章  >  后端开发  >  在 PHP 中重写

在 PHP 中重写

WBOY
WBOY原创
2024-08-29 12:59:101029浏览

重写是一个面向对象编程概念,类似于 PHP 中的类、对象、封装、多态、重载等概念。当派生类中创建与基类或父类中的方法相同的方法时,就会完成函数和类的重写。这两个方法具有相同的名称和相同数量的参数。

广告 该类别中的热门课程 PHP 开发人员 - 专业化 | 8 门课程系列 | 3次模拟测试

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

覆盖如何工作?

让我们探索一下 PHP 中的重写是如何工作的。

  • 重写是一个概念,其中基类的派生类执行与基类相同的操作。这种重写可以在方法或类上。如果是方法重写,那么方法的名称、参数、访问说明符会发现与父类方法的访问说明符相同。如果从基类派生的子类中没有发现该方法的实现发生变化,则称该子类的方法已经覆盖了基类的方法。
  • 假设有一个名为 Person 的类,它有自己的数据属性和方法。这是具有名为 talk() 的方法的父类或基类 Person。现在又多了一个名为 Employee 的类,它扩展了 Person 类。现在,这个 Employee 类拥有自己的数据属性和方法,并且还具有与父类相同的方法,例如.speak()。
  • 这个speaks()方法存在于基类中,也存在于派生类中。
  • 现在发生的是当类实例化或创建对象时,将执行speak()方法中的哪一个,这意味着执行speak 基类或speak 派生类取决于对象类的名称被称为。
  • 换句话说,如果创建了 Person 类的对象,则调用并执行 Person 类的 talk() 方法。但如果创建了 Employee 类的对象,则将执行派生类的 talk() 方法,该方法会覆盖父类的 talk() 类。
  • 下面是如何通过示例进行覆盖
  • 在下面的示例中,有两个类,基类和派生类。派生类扩展了基类。这些类被启动并创建两个对象 $obj1 和 $obj2。 $obj1 是 BaseClass 的对象,$obj2 是派生类的对象。这些对象进一步调用各自类中声明的方法。
  • 在这里您将观察到基类和派生类具有相同的方法,称为 ABC()。当你执行这个程序时,你会注意到 ABC() 方法已经覆盖了基类方法 ABC()。

方法重写示例

方法重写的例子如下。

代码:

class BaseClass {
public function ABC() {
echo "<br /> In the base class";
}
}
class DerivedClass extends BaseClass {
// override the method ABC() of base class
public function ABC() {
echo "<br />In the derived class";
}
}
$obj1 = new BaseClass;
$obj1->ABC();
$obj2 = new DerivedClass;
$obj2->ABC();

输出:

在 PHP 中重写

使用访问修饰符覆盖

共有三个访问修饰符。

  1. 公共:可从程序中的任何位置访问。
  2. 私有:只能从父类访问。
  3. 受保护:可从基类和派生类访问。

众所周知,受保护的方法可以从基类和派生类访问,它可以在子类中公开,但不能私有,因为私有方法只能在父类中访问。此外,如果类方法的访问说明符为 public,则派生类中的重写方法不能声明为 private 和 protected

使用访问修饰符覆盖方法的示例

下面写了一个使用访问修饰符进行覆盖的示例。

代码:

class BaseClass {
private function ABC() {
echo "<br/>In the base class Method : ABC";
}
protected function XYZ() {
echo "<br/>In the base class Method : XYZ";
}
}
class DerivedClass extends BaseClass {
// overriding with public for wider accessibility
public function ABC() {
echo "<br/> In the derived class Method : ABC";
}
// overriding method
// with more accessibility
public function XYZ() {
echo "<br/>In the derived class Method : XYZ";
}
}
//$obj1 = new BaseClass;
//$obj1->ABC();   //throws fatal error
//$obj1->XYZ();          //throws fatal error
$obj2 = new DerivedClass;
$obj2->ABC();
$obj2->XYZ();

输出:

在 PHP 中重写

使用 Final 关键字覆盖

final 关键字用于类和方法。可以覆盖方法和类,而不是变量。

最终方法重写

当一个方法或类被声明为final时,则无法对该方法或类进行重写,并且该类的继承也是不可能的。

使用 Final 关键字覆盖方法的示例

使用final关键字覆盖的例子如下。

代码:

class BaseClass {
// Final method – display
// this cannot be overridden in base class
final function display() {
echo "<br /> In the Base class display function";
}
/// method - ABC
function ABC() {
echo "<br /> In the Base cLass ABC function";
}
}
class DerivedClass extends BaseClass {
function ABC() {
echo "<br /> In the Derived class ABC function";
}
}
$obj1 = new DerivedClass;
$obj1->display();
$obj1->ABC();

输出:

在 PHP 中重写

Final Class Overriding

A class declared as final cannot be inherited. A Final Class further have final method along with other methods. But since the class itself is declared final there is no use of declaring a final method in a final class.

Example of Class Overriding using Final Keyword

The example of class overriding using final keyword is written below.

Code:

// class declared as final cannot be overridden
final class BaseClass {
// method - ABC
function ABC() {
echo "<br> In the BaseClass Method ABC function";
}
// Final method - display
function display() {
echo "<br> In the BaseClass Method display function";
}
}
// here you cannot extend the base class
// as the base class is declared as final
$obj1 = new BaseClass;
$obj1->display();
$obj1->ABC();

Output:

在 PHP 中重写

以上是在 PHP 中重写的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
上一篇:PHP XOR下一篇:Method Overloading in PHP