How to implement polymorphism in php

青灯夜游
Release: 2023-03-10 09:24:02
Original
4399 people have browsed it

PHP methods to achieve polymorphism: 1. Implement polymorphism by implementing interfaces; 2. Use interfaces and combinations to set another class as an attribute in one class to simulate multiple inheritance, and achieve multiple inheritance through inheritance relationships. state.

How to implement polymorphism in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

Polymorphism refers to the same operation or function ,Procedures can act on many types of objects and obtain ,different results. Different objects can produce different results when receiving the same message. This phenomenon is called polymorphism.

Polymorphism allows each object to respond to common messages in a way that suits itself. Polymorphism enhances software flexibility and reusability.

In object-oriented software development, polymorphism is one of the most important parts. Object-oriented programming is not just about simply combining related methods and data, but using various elements in object-oriented programming to clearly describe various situations in real life. This section will provide a detailed explanation of polymorphism in object-oriented programming.

What is polymorphism

Polymorphism (Polymorphism) literally means "multiple shapes". It can be understood as multiple forms of expression, that is, "one external interface and multiple internal implementation methods." In object-oriented theory, the general definition of polymorphism is: the same operation will produce different execution results when applied to instances of different classes. That is, when objects of different types receive the same message, they will get different results.

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 that exist between different subclass objects can be shielded. Differences, write common code, and make common programming to adapt to changing needs.

Polymorphic implementation conditions

There are three conditions for polymorphic implementation: First, there must be inheritance, that is, there must be a parent class (or base class ) and its derived subclasses. Secondly, there must be a reference from the parent class pointing to the object of the subclass, which is the most important condition for achieving polymorphism. Finally, there must be method rewriting, that is, the subclass must rewrite some methods of the parent class according to its own needs. The method names and parameters are the same.

How to achieve polymorphism in php:

1. Implement polymorphism by implementing interfaces

In the following example, the static method of the UserAdmin class requires a User type parameter.

In subsequent use, an instance of the NormalUser class that implements the User interface is passed. The code runs successfully.

<?
interface User{ // User接口
	public function  getName();
	public function setName($_name);
}
class NormalUser implements User { // 实现接口的类.
	private $name;
	public function getName(){
		return $this->name;
	}
	public function setName($_name){
		$this->name = $_name;
	}
}
class UserAdmin{ //操作.
	public static function  ChangeUserName(User $_user,$_userName){
		$_user->setName($_userName);
	}
}
$normalUser = new NormalUser();
UserAdmin::ChangeUserName($normalUser,"Tom");//这里传入的是 NormalUser的实例.
echo $normalUser->getName();
?>
Copy after login

Program running results:

Tom
Copy after login
Copy after login

2. Implement polymorphism through inheritance relationship

Use interfaces and combinations to add another class to one class Classes are set as attributes to simulate multiple inheritance and achieve polymorphism through inheritance relationships.

The following is the relationship between classes and subclasses.

<?
class User{ // User接口
	public function  getName(){}
}
class NormalUser extends User { // 继承自User类
	private $name;
	public function getName(){
		return $this->name;
	}
	public function setName($_name){
		$this->name = $_name;
	}
}
class UserAdmin{ //操作.
	public static function  ChangeUserName(User $_user,$_userName){
		$_user->setName($_userName);
	}
}
$normalUser = new NormalUser();
UserAdmin::ChangeUserName($normalUser,"Tom");//这里传入的是 NormalUser的实例.
echo $normalUser->getName();
?>
Copy after login

Program running results:

Tom
Copy after login
Copy after login

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to implement polymorphism 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