PHP5中实现多态的两种方法实例分享_php实例
在PHP5中,变量的类型是不确定的,一个变量可以指向任何类型的数值、字符串、对象、资源等。我们无法说PHP5中多态的是变量。
我们只能说在PHP5中,多态应用在方法参数的类型提示位置。
一个类的任何子类对象都可以满足以当前类型作为类型提示的类型要求。
所有实现这个接口的类,都可以满足以接口类型作为类型提示的方法参数要求。
简单的说,一个类拥有其父类、和已实现接口的身份。
通过实现接口实现多态
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();
?>
使用接口与组合模拟多继承
通过组合模拟多重继承。
在PHP中不支持多重继承,如果我们向使用多个类的方法而实现代码重用有什么办法么?
那就是组合。在一个类中去将另外一个类设置成属性。
下面的例子,模拟了多重继承。
接口实例
写一个概念性的例子。 我们设计一个在线销售系统,用户部分设计如下: 将用户分为,NormalUser, VipUser, InnerUser 三种。要求根据用户的不同折扣计算用户购买产品的价格。并要求为以后扩展和维护预留空间。
interface User
{
public function getName();
public function setName($_name);
public function getDiscount();
}
abstract class AbstractUser implements User
{
private $name = "";
protected $discount = 0;
protected $grade = "";
function __construct($_name) {
$this->setName($_name);
}
function getName() {
return $this->name;
}
function setName($_name) {
$this->name = $_name;
}
function getDiscount() {
return $this->discount;
}
function getGrade() {
return $this->grade;
}
}
class NormalUser extends AbstractUser
{
protected $discount = 1.0;
protected $grade = "Normal";
}
class VipUser extends AbstractUser
{
protected $discount = 0.8;
protected $grade = "VipUser";
}
class InnerUser extends AbstractUser
{
protected $discount = 0.7;
protected $grade = "InnerUser";
}
interface Product
{
function getProductName();
function getProductPrice();
}
interface Book extends Product
{
function getAuthor();
}
class BookOnline implements Book
{
private $productName;
protected $productPrice;
protected $Author;
function __construct($_bookName) {
$this->productName = $_bookName;
}
function getProductName() {
return $this->productName;
}
function getProductPrice() {
$this->productPrice = 100;
return $this->productPrice;
}
public function getAuthor() {
$this->Author = "chenfei";
return $this->Author;
}
}
class Productsettle
{
public static function finalPrice(User $_user, Product $_product, $number) {
$price = $_user->getDiscount() * $_product->getProductPrice() * $number;
return $price;
}
}
$number = 10;
$book = new BookOnline("设计模式");
$user = new NormalUser("tom");
$price = Productsettle::finalPrice($user, $book, $number);
$str = "您好,尊敬的" . $user->getName() . "
";
$str .= "您的级别是" . $user->getGrade() . "
";
$str .= "您的折扣是" . $user->getDiscount() . "
";
$str .= "您的价格是" . $price;
echo $str;
?>

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

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



The differences between php5 and php8 are in terms of performance, language structure, type system, error handling, asynchronous programming, standard library functions and security. Detailed introduction: 1. Performance improvement. Compared with PHP5, PHP8 has a huge improvement in performance. PHP8 introduces a JIT compiler, which can compile and optimize some high-frequency execution codes, thereby improving the running speed; 2. Improved language structure, PHP8 introduces some new language structures and functions. PHP8 supports named parameters, allowing developers to pass parameter names instead of parameter order, etc.

Virtual base classes optimize vtable memory overhead by allowing inheritance from multiple base classes without creating additional vtables. In the optimized code, the shape base class no longer has a virtual function table, and the circle and rectangle classes share the same virtual function table, thus reducing memory consumption.

How to implement polymorphic storage and multidimensional query of data in MySQL? In practical application development, polymorphic storage and multidimensional query of data are a very common requirement. As a commonly used relational database management system, MySQL provides a variety of ways to implement polymorphic storage and multidimensional queries. This article will introduce the method of using MySQL to implement polymorphic storage and multi-dimensional query of data, and provide corresponding code examples to help readers quickly understand and use it. 1. Polymorphic storage Polymorphic storage refers to the technology of storing different types of data in the same field.

How to change port 80 in php5: 1. Edit the port number in the Apache server configuration file; 2. Edit the PHP configuration file to ensure that PHP works on the new port; 3. Restart the Apache server, and the PHP application will start running on the new port. run on the port.

How to use polymorphism and inheritance to handle data types in PHP Introduction: In PHP, polymorphism and inheritance are two important object-oriented programming (OOP) concepts. By using polymorphism and inheritance, we can handle different data types more flexibly. This article will introduce how to use polymorphism and inheritance to deal with data types in PHP, and show their practical application through code examples. 1. The basic concept of inheritance Inheritance is an important concept in object-oriented programming. It allows us to create a class that can inherit the properties and methods of the parent class.

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

PHP is a server-side programming language that supports object-oriented programming (OOP) since PHP5. The core idea of OOP is to encapsulate data and behavior in objects to improve the maintainability and scalability of the program. In PHP, object-oriented programming has three major characteristics: inheritance, polymorphism and interfaces. 1. Inheritance Inheritance means that a class can inherit properties and methods from another class. The inherited class is called the parent class or base class, and the inherited class is called the subclass or derived class. Subclasses can obtain the properties and methods in the parent class through inheritance, and can

Solution steps for php5 not listening to port 9000: 1. Check the PHP-FPM configuration file; 2. Restart the PHP-FPM service; 3. Turn off the firewall or configure port forwarding; 4. Check whether other processes occupy port 9000.
