Home > Backend Development > PHP Tutorial > Detailed explanation of object-oriented polymorphic examples in PHP

Detailed explanation of object-oriented polymorphic examples in PHP

小云云
Release: 2023-03-21 16:34:01
Original
1277 people have browsed it

This article mainly shares with you a detailed explanation of PHP's object-oriented polymorphism examples. Polymorphism refers to the ability to redefine or change the nature and behavior of a class according to the context of using the class in object-oriented. PHP does not support overloading to achieve polymorphism. , but PHP can achieve polymorphic effects in different directions.

is as follows:

class a{    function test($i){ // $i可以是任何类型的变量
        print_r $i;
    }
}
Copy after login

The above example shows that since PHP is a weakly typed language, $i can be any type of variable. In this way, a function can implement a strongly typed language such as Java. The polymorphic form of overloaded methods relies on changing parameter types.

This form is more convenient and efficient than JAVA's parameter type overloading, but it also has problems, as follows:

<?php/**
教师类有一个drawPolygon()方法需要一个Polygon类,用来画多边形,此方法内部调用多边形的draw()方法,但由于弱类型,我们可以传入Circle类,就会调用Circle类的draw方法,这就事与愿违了。甚至可以传入阿猫、阿狗类,如果这些类没有draw()方法还会报错。*/class Teacher{    function drawPolygon($polygon){ 
        $polygon->draw(); 
    }    
}class Polygon{    function draw(){        echo "draw a polygon";
    }
}class Circle{    function draw(){        echo "draw a circle";
    }
}?>
Copy after login

It can be seen that such flexible polymorphism requires some control. In PHP5. 3. In the future, you can impose type restrictions on parameters, as follows:

// 仿java,在变量参数前加一个限制类名
    function drawPolygon(Polygon $polygon){ 
        $polygon->draw(); 
    }
Copy after login

This restricts only Polygon and its subclasses to be passed in.

There is also an overload that changes the number of parameters. This is also because PHP does not support method overloading, so some alternative methods are needed to implement it, as follows:

<?php// 通过可变参数来达到改变参数数量重载的目的
// 不是必须传入的参数,必须在函数定义时赋初始值function open_database($DB, $cache_size_or_values=null, $cache_size=null)
{    switch (function_num_args())  
// 通过function_num_args()函数计算传入参数的个数,根据个数来判断接下来的操作   
 {        case 1:            
 $r = select_db($DB);           
  break;        case 2:           
   $r = select_db($DB, $cache_size_or_values);           
    break;        case 3:           
     $r = select_db($DB, $cache_size_or_values, $cache_size);            
     break;
    }    return is_resource($r);
}?>
Copy after login

Related recommendations:

Introduction to PHP object-oriented inheritance, polymorphism, and encapsulation

PHP polymorphism and dynamic binding

A case of php implementing object-oriented polymorphism method

The above is the detailed content of Detailed explanation of object-oriented polymorphic examples in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template