首页  >  文章  >  后端开发  >  PHP 中的多态性

PHP 中的多态性

PHPz
PHPz原创
2024-08-29 13:05:50304浏览

多态性是 PHP 面向对象编程语言特性(OOPS 特性)的好概念之一。多态性一词实际上源自两个希腊词“Poly”和“Morph”。 Poly 的意思是“许多”,morph 的意思是“形式”。它意味着具有多种形式的能力。一般来说,多态性是一种面向对象的编程模式,其中类具有各种功能,同时具有公共接口。实际上多态有两种类型:编译时多态和运行时多态。 PHP 不支持编译时多态性,这意味着函数和运算符重载。

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

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

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

语法:

Class a{
//methods
Public function b(){
//methods
}
}
Class b extends a{
//methods
Public function b(){
//methods which are in/not in the function of the class a.
}
}

说明:

在上面的语法中,我们创建了 2 个类,一个是普通类,另一个是扩展类,但我们使用相同的函数名称/方法来实现不同的功能。这称为多变形。在编码中可以根据需要使用许多具有不同功能的类似名称的函数/方法。

PHP 中的多态函数

PHP 的多态性基于运行时多态性概念。在 PHP 中,多态性是通过在运行时做出决定来工作的,这实际上不是编译时多态性概念,否则我们可以说我们有许多/多个实现,它们是超类的子类型,运行时(函数重载)概念是一个PHP 编程语言多态性的示例。

函数重载是指从新创建的函数派生出一个类,该函数在其父类中具有相同的签名(这意味着该函数具有相同的名称、相同的类型和多个参数)作为函数,称为方法重写。在多态概念中,可以通过扩展来创建函数,但必须涉及相同名称/相似命名的函数,以便处理多态和继承概念。为了通过增加类的功能来增强编程能力,具有公共接口的多态性是必须的。

在 PHP 中实现多态性的示例

以下是 PHP 中多态性的示例:

示例#1

在下面的示例中,创建了一个基类“Shap1”。这里我们要继承三个派生类中的Shap1类。类名称为 Circle1、Triangle1 和 Elliipse1。每个类都有draw函数,以完成PHP编程语言的运行时多态性。在下面的调用过程中,我们将创建一个长度为2的数组,每个数组的索引有助于创建一个类的对象。之后,我们将使用一个循环,该循环将根据数组的长度执行,该数组的值“$i1”传递给对象数组变量“$Val1[$i1]”。所以它会被执行3次,并且会使用每个类的draw()方法来调用它。

代码:

<?php
class Shap1
{
function draw1(){}
}
class Circle1 extends Shap1
{
function draw1()
{
print "Circle1 has been drawn.\n";
}
}
class Triangle1 extends Shap1
{
function draw1()
{
print "Triangle1 has been drawn.\n";
}
}
class Ellipse1 extends Shap1
{
function draw1()
{
print "Ellipse1 has been drawn.";
}
}
$Val1=array(2);
$Val1[0]=new Circle1();
$Val1[1]=new Triangle1();
$Val1[2]=new Ellipse1();
for($i1=0;$i1<3;$i1++)
{
$Val1[$i1]->draw1();
}
?>

输出:

PHP 中的多态性

示例#2

这是通过界面帮助实现多态性概念的示例。接口与任何类非常相似,除了不包含任何代码。该接口有助于定义方法名称和方法参数,但这里不会定义方法的内容。任何类执行的接口都必须由该接口所表征的所有方法执行。

这里 Machine1 将拥有定义 calcTask1 的所有类。 Circle1 实现 CallTask​​1() 方法。 Rectangle1 还尝试实现 Machine 接口,但使用不同的 calcTask1() 方法体进行定义。多态性表示所有计算任务的方法都应该有一个等效的名称。我们将调用 calcTask1() 的不同名称,但不提供详细信息。这里计算任务的方法的名称很重要。

代码:

<?php
interface Machine1 {
public function calcTask1();
}
class Circle1 implements Machine1 {
private $radius1;
public function __construct($radius1){
$this -> radius1 = $radius1;
}
public function calcTask1(){
return $this -> radius1 * $this -> radius1 * pi();
}
}
class Rectangle1 implements Machine1 {
private $width1;
private $height1;
public function __construct($width1, $height1){
$this -> width1 = $width1;
$this -> height1 = $height1;
}
public function calcTask1(){
return $this -> width1 * $this -> height1;
}
}
$mycirc1 = new Circle1(3);
$myrect1 = new Rectangle1(3,4);
echo $mycirc1->calcTask1();
echo $myrect1->calcTask1();
?>

输出:

PHP 中的多态性

Example #3

In the below example a class “base11” is created with a function add1() with $a1 and $b1 variables. Then “$res1” variable is created which is used to store the multiplication of the two variables “$a1” and “$b1” and the echo statement is used to print if the function is called. Then Child1 class is used to extend the base11 class. In the extended class, again add1() variable is created to store the sum or the a1 and b1 variables. Then a new object is created and called by passing the variable values to the values $a1 and $b1 variables.

Code:

<?php
class base11
{
function add1($a1,$b1)
{
$res1=$a1*$b1;
echo "Multiplication = ".$res1;
}
}
class child1 extends base11
{
function add1($a1,$b1)
{
$res1=$a1+$b1;
echo "Sum  = ".$res1;
}
}
$obj= new child1();
$obj->add1(1000,500);
?>

Output:

PHP 中的多态性

Example #4

In the below example a class “BaseClass11” is created with the public function “myMethod()” is created with the echo statement to mention that “BaseClass11 method” is called that. Then again DerivedClass11 is created to extend the BaseClass11 with the same name function “myMethod()” with the echo statement to print “DerivedClass11 method called” like that. We are calling the same/similar naming function in different classes. Then new object is created for the function/ method “myMethod()”.

Code:

<?php
class BaseClass11
{
public function myMethod11()
{
echo "BaseClass11 method called";
}
}
class DerivedClass11 extends BaseClass11
{
public function myMethod11()
{
echo "DerivedClass11 method called";
}
}
function processClass11(BaseClass11 $c11)
{
$c11->myMethod11();
}
$c11 = new DerivedClass11();
processClass11($c11);
?>

Output:

PHP 中的多态性

Recommended Article

This is a guide to the Polymorphism in PHP. Here we discuss the what is the definition of Polymorphism and its Working along with its examples as well as Code Implementation. You can also go through our other suggested articles to learn more-

  1. PHP Frameworks
  2. basename in PHP
  3. Validation in PHP
  4. print_r() in PHP

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

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