Home  >  Article  >  Backend Development  >  How to implement polymorphism in php

How to implement polymorphism in php

青灯夜游
青灯夜游Original
2021-06-02 15:24:354393browse

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();
?>

Program running results:

Tom

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();
?>

Program running results:

Tom

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!

Statement:
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