


PHP object-oriented programming - in-depth understanding of method overloading and method coverage (polymorphism), polymorphism coverage_PHP tutorial
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

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Introduction In today's rapidly evolving digital world, it is crucial to build robust, flexible and maintainable WEB applications. The PHPmvc architecture provides an ideal solution to achieve this goal. MVC (Model-View-Controller) is a widely used design pattern that separates various aspects of an application into independent components. The foundation of MVC architecture The core principle of MVC architecture is separation of concerns: Model: encapsulates the data and business logic of the application. View: Responsible for presenting data and handling user interaction. Controller: Coordinates the interaction between models and views, manages user requests and business logic. PHPMVC Architecture The phpMVC architecture follows the traditional MVC pattern, but also introduces language-specific features. The following is PHPMVC

SOLID principles are a set of guiding principles in object-oriented programming design patterns that aim to improve the quality and maintainability of software design. Proposed by Robert C. Martin, SOLID principles include: Single Responsibility Principle (SRP): A class should be responsible for only one task, and this task should be encapsulated in the class. This can improve the maintainability and reusability of the class. classUser{private$id;private$name;private$email;publicfunction__construct($id,$nam

PHP extensions can support object-oriented programming by designing custom functions to create objects, access properties, and call methods. First create a custom function to instantiate the object, and then define functions that get properties and call methods. In actual combat, we can customize the function to create a MyClass object, obtain its my_property attribute, and call its my_method method.

PHP's object-oriented programming paradigm provides advantages for project management and organization With the rapid development of the Internet, websites and applications of all sizes have sprung up. In order to meet the growing needs and improve development efficiency and maintainability, the use of object-oriented programming (Object-Oriented Programming, OOP for short) has become the mainstream of modern software development. In dynamic scripting languages like PHP, OOP brings many advantages to project management and organization. This article will introduce

In high-concurrency scenarios of object-oriented programming, functions are widely used in the Go language: Functions as methods: Functions can be attached to structures to implement object-oriented programming, conveniently operating structure data and providing specific functions. Functions as concurrent execution bodies: Functions can be used as goroutine execution bodies to implement concurrent task execution and improve program efficiency. Function as callback: Functions can be passed as parameters to other functions and be called when specific events or operations occur, providing a flexible callback mechanism.

What is object-oriented programming? Object-oriented programming (OOP) is a programming paradigm that abstracts real-world entities into classes and uses objects to represent these entities. Classes define the properties and behavior of objects, and objects instantiate classes. The main advantage of OOP is that it makes code easier to understand, maintain and reuse. Basic Concepts of OOP The main concepts of OOP include classes, objects, properties and methods. A class is the blueprint of an object, which defines its properties and behavior. An object is an instance of a class and has all the properties and behaviors of the class. Properties are characteristics of an object that can store data. Methods are functions of an object that can operate on the object's data. Advantages of OOP The main advantages of OOP include: Reusability: OOP can make the code more

Functional and Object-Oriented Programming (OOP) provide different programming mechanisms in C++: Function: independent block of code, focused on performing a specific task, containing no data. OOP: Based on objects, classes and inheritance, data and behavior are encapsulated in objects. In actual cases, the function method for calculating the area of a square is simple and direct, while the OOP method encapsulates data and behavior and is more suitable for managing object interaction. Choosing the appropriate approach depends on the scenario: functions are good for independent tasks, OOP is good for managing complex object interactions.

1. Introduction to Python Python is a general-purpose programming language that is easy to learn and powerful. It was created by Guido van Rossum in 1991. Python's design philosophy emphasizes code readability and provides developers with rich libraries and tools to help them build various applications quickly and efficiently. 2. Python basic syntax The basic syntax of Python is similar to other programming languages, including variables, data types, operators, control flow statements, etc. Variables are used to store data. Data types define the data types that variables can store. Operators are used to perform various operations on data. Control flow statements are used to control the execution flow of the program. 3.Python data types in Python
