


PHP object-oriented programming - in-depth understanding of method overloading and method coverage (polymorphism), polymorphism coverage
What is polymorphism?
Polymorphism literally means "multiple states". In object-oriented languages, multiple different implementations of an interface are called polymorphism. Quoting Charlie Calverts' description of polymorphism - Polymorphism is a technique that allows you to set a parent object to be equal to one or more of its child objects. After assignment, the parent object can be assigned to its child objects based on the current value. features operate in different ways (from "Insider Delphi 4 Programming Technology"). To put it simply, it is one sentence: It is allowed to assign a pointer of a subclass type to a pointer of a parent class type (yes, this passage comes from Baidu Encyclopedia). So what is the role of polymorphism, and what is its actual development value? In actual application development, the main purpose of using object-oriented polymorphism is that different subclass objects can be treated as one parent class, and the differences between different subclass objects can be shielded and universal objects can be written. Code, making general programming to adapt to changing needs.
The following are two implementations of polymorphism in PHP
Method overload (overload)
Overloading is an implementation of class polymorphism. Function overloading means that an identifier is used as multiple function names and can be passed through the function's number of parameters or parameter type Distinguish these functions with the same name so that there is no confusion when calling. That is, when called, although the method names are the same, the corresponding functions can be automatically called according to different parameters.
class A{ public function test(){ echo "test1"; } public function test($a){ echo "test2"; } } $a=new A(); $a->test(); $a->test($a);
If php directly supports method overloading. Then after the above example is executed, different values will be returned if parameters are passed and if no parameters are passed. However, php does not directly support overloading , which means that if you define it directly as above, an error will be reported. What error will be reported? The following error will be reported.
This means that function A cannot be defined repeatedly, and the number of lines reporting the error is exactly the following line.
public function test($a){
So php does not directly support reloading. The co-author has been saying for a long time that php does not support it. . Don't worry, what I said is that it is not directly supported, so we can let php support it indirectly. At this time, a function will be used to support overloading. It's __call(). The __call() method must take two parameters. The first one contains the name of the method being called, while the second parameter contains the array of parameters passed to the method. Functions similar to function overloading can be achieved through this method. Look at the code below.
public function __call($method,$p<span>) { if($method=="display"<span>){ if(is_object($p[0<span>])){ $this->displayObject($p[0<span>]); }else if(is_array($p[0<span>])){ $this->displayArray($p[0<span>]); }else<span>{ $this->displayScalar($p[0<span>]); } } }<br /> //下面是对上面定义的调用 $ov=new<span> overload; $ov->display(array(1,2,3<span>)); $ov->display('cat');</span></span></span></span></span></span></span></span></span></span>
When defining a method, you can see that there are three branches. If an object is passed to the display() method, the displayObject() method is called; if an array is passed, displayArray() is called; pass If it contains other content, the displayScalar() method is called. . . You can see that when calling below, the first one is to pass an array, then displayArray() is called. The second one passed in is neither an object nor an array, it belongs to other content, and the displayScalar() method is called. So in this way, the __call() method is used to implement method overloading similar to other languages.
Method override
The so-called overwriting is essentially rewriting. That is, when a subclass inherits some methods from the parent class, and the subclass defines the same method internally, the newly defined method will override the inherited method from the parent class, and the subclass can only call its internally defined methods. method.
has the following requirements:
1. When a parent class and a subclass have a method with exactly the same parameters and names, then the subclass method will override the parent class method.
2. When implementing method coverage, the access modifiers can be different, but the access scope of the subclass must be greater than or equal to the access scope of the parent class.
3. The parameters are required to be the same as the name. It is not required that the subclass has the same name as the parent class.
The following is an explanation of these points:
The first point is that the parameters must be consistent to achieve method coverage. When the number of parameters is inconsistent, an error will be reported (this involves the overloading of the above-mentioned methods). When the method names are inconsistent, they will not be overwritten, only the newly defined methods of the subclass. ;
The second point is that this is the design rule of languages such as php. What I understand is that it is easier to access things at a higher level. If you want to access things at a lower level, you must have higher permissions.
Look at the code:
class<span> people{ protected function<span> sing(){ echo "人唱歌"<span>; } } class woman extends<span> people{ public function<span> sing(){ echo "女人唱歌"<span>; } } $woman1=new<span> woman(); $woman1->sing();</span></span></span></span></span></span></span>
This is a normal way to output "women singing". But when the sing() method in woman is changed to proctcted and the parent element is changed to public(), that is, after the access permission of the parent class is set to be greater than that of the subclass, the following error will be reported.
The third point is that the parameters are required to be the same as their names. Specifically, the number of parameters is required to be the same as the parent class, not the parameters The name is consistent. That is, the name of the parameters passed can be arbitrary, as long as the number passed is the same.
The above introduces two implementations of polymorphism in the PHP language.Well, that’s pretty much it. .
http://www.bkjia.com/PHPjc/1083560.html

Java作为一门常见的编程语言,其在IT行业中有着广泛的应用,成为了许多公司招聘中的重要技能之一。在Java开发岗位的面试中,面试官往往会问及一些常见的Java问题,以此考察应聘者的Java编程水平。本文将列举几个常见的Java面试问题及其答案,供广大应聘者参考。什么是Java虚拟机?答:Java虚拟机(JVM)是一种虚拟的计算机,可以执行Java字节码。它

PHP的面向对象编程范式为项目管理和组织提供优势随着互联网的飞速发展,各种规模的网站和应用程序如雨后春笋般涌现出来。为了满足日益增长的需求,并提高开发效率和可维护性,采用面向对象编程(Object-OrientedProgramming,简称OOP)的方法成为了现代软件开发的主流。在PHP这样的动态脚本语言中,OOP为项目管理和组织带来了许多优势,本文将介

随着计算机应用的不断普及和发展,程序设计语言也不断演化和更新。面向对象编程语言在当今程序设计中占据了主导地位,而MySQL则是最流行的关系型数据库管理系统之一。如何在面向对象编程中结合使用MySQL实现更优雅的代码呢?一、面向对象编程的基本概念面向对象编程(ObjectOrientedProgramming,简称OOP)是一种编程思想,它强调对象的概念和

Java是一种面向对象编程语言,具备三个重要的特性:封装、继承和多态。这三个特性使得Java程序具有高度的可重用性、可维护性和可扩展性。在本文中,我们将介绍Java面向对象编程的三个重要特性。一、封装封装是面向对象编程的基础。它是指将数据和行为封装在一起,保护数据不被直接访问,而是通过类的接口进行访问。封装可以使得程序更加安全、可靠和易于维护。在Java中,

PHP面向对象编程中的访问者模式解析访问者模式是一种常用的设计模式,它可以分离数据结构和处理逻辑,使得同一个数据结构可以有不同的处理逻辑,而且可以在不修改数据结构的情况下增加新的处理逻辑。在PHP中,访问者模式可以帮助我们更好地组织代码,并提高代码的可维护性和可扩展性。本文将深入探讨PHP面向对象编程中的访问者模式,并通过示例代码进行解析。一、模式概述访问者

面向对象编程是一种编码设计,它使用数据来表示一组指令。它是一种具有对象概念的程序编程典范,同时也是一种程序开发的抽象方针。它由描述状态的属性和用来实现对象行为的方法组成,完成了从数据模型到处理模型的结合与统一。

理解PHP面向对象编程中的工厂模式工厂模式是一种常用的设计模式,它用于创建对象的过程中将对象的创建和使用解耦。在PHP面向对象编程中,工厂模式可以帮助我们更好地管理对象的创建和生命周期。本文将通过代码示例来详细介绍PHP中的工厂模式。在PHP中,我们可以通过使用工厂模式来实现对象的创建和初始化过程,而不是直接使用new关键字。这样做的好处是,如果将来需要改变

解析PHP面向对象编程中的类属性和方法PHP是一种被广泛应用于Web开发的脚本语言,它支持面向对象编程(OOP)的特性。在PHP中,类是一种用来创建对象的蓝图或模板,而属性和方法则是类的核心部分。本文将深入解析PHP面向对象编程中的类属性和方法,并通过代码示例来加深理解。一、类属性类属性是指用于描述类的特有数据的变量。它们可以存储对象的状态和特征。在PHP中


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version
Chinese version, very easy to use
