PHP5多态性与动态绑定介绍,php5多态动态绑定
PHP5多态性与动态绑定介绍,php5多态动态绑定
什么是多态性?
多态性是继数据抽象和继承后,面向对象语言的第三个特征。从字面上理解,多态的意思是“多种形态”,简单来说,多态是具有表现多种形态的能力的特征,在OO中是指“语言具有根据对象的类型以不同方式处理之,特别是重载方法和继承类这种形式”的能力。多态被认为是面向对象语言的必备特性。
例如:
我们创建一个接口 Shape,定义一个空的方法 draw(),那么所有的实现类都必须实现这个方法,假设 Shape 有两个实现类:Triangle 和 Rectangle,我们虽然无法通过类似这样的 Java 代码来诠释 PHP 的多态性:
复制代码 代码如下:
Shape s = new Triangle();
s.draw();
不过 PHP5.1 中引入了 Type Hinting,可以限制函数(或者方法)的参数类型,我们使用这个特性来演示 PHP5 的多态性。
参考如下的代码:
复制代码 代码如下:
class TestPolymorphism {
public function drawNow(Shape $shape) {
$shape->draw();
}
}
函数 drawNow() 中限制传入的参数类型必须为 Shape 接口派生类的对象,这里我们传递给 drawNow() 的参数可能是 Triangle 或者 Rectangle 的对象,也可能是其它的 Shape 接口的派生类对象,比如 Circle 等等,简单的说 drawNow() 的参数类型是无法预知的,$shape->draw() 的行为最终由传入的参数的具体类型来决定,比如如果传入 Triangle 的对象,那么就调用 Triangle 的 draw() 方法,如果传入 Rectangle 的对象,就调用 Rectangle 的 draw() 方法。这种在运行时刻根据传递的对象参数的类型来决定调用哪一个对象的方法的行为就可以称之为多态。
Shape 也可以是一个抽象基类或者是非抽象的基类,上面的论述都是成立的。区别在于接口仅定义一套实现类必须遵守的规则,而使用基类则可以为派生类提供一些缺省的行为。
参考代码如下:
复制代码 代码如下:
/**
* Shape Interface
*
* @version 1.0
* @copyright
*/
interface Shape {
public function draw();
}
/**
* Triangle
*
* @uses Shape
* @version 1.0
* @copyright
*/
class Triangle implements Shape {
public function draw() {
print "Triangle::draw()\n";
}
}
/**
* Rectangle
*
* @uses Shape
* @version 1.0
* @copyright
*/
class Rectangle implements Shape {
public function draw() {
print "Rectangle::draw()\n";
}
}
/**
* Test Polymorphism
*
* @version 1.0
* @copyright
*/
class TestPoly {
public function drawNow(Shape $shape) {
$shape->draw();
}
}
$test = new TestPoly();
$test->drawNow(new Triangle());
$test->drawNow(new Rectangle());
/* vim: set expandtab tabstop=4 shiftwidth=4: */
什么是动态绑定?
HaoHappy 翻译的 PHP5 Object Pattern 第九节中有介绍:
除了限制访问,访问方式也决定哪个方法将被子类调用或哪个属性将被子类访问。 函数调用与函数本身的关联,以及成员访问与变量内存地址间的关系,称为绑定。
另有的说法:
绑定(binding):将方法的调用连到方法本身被称为绑定,当绑定发生在编译期,被称做静态绑定,而在程序运行的时候根据对象的类型来决定该绑定方法的成为动态绑定。
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

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.

Inheritance and polymorphism affect the coupling of classes: Inheritance increases coupling because the derived class depends on the base class. Polymorphism reduces coupling because objects can respond to messages in a consistent manner through virtual functions and base class pointers. Best practices include using inheritance sparingly, defining public interfaces, avoiding adding data members to base classes, and decoupling classes through dependency injection. A practical example showing how to use polymorphism and dependency injection to reduce coupling in a bank account application.

How to dynamically bind and update form data in Vue With the continuous development of front-end development, forms are an interactive element that we often use. In Vue, dynamic binding and updating of forms is a common requirement. This article will introduce how to dynamically bind and update form data in Vue, and provide specific code examples. 1. Dynamic binding of form data Vue provides the v-model instruction to achieve two-way binding of form data. Through the v-model directive, we can compare the value of the form element with the Vue instance

Destructors are crucial in C++ polymorphism, ensuring that derived class objects properly clean up memory when they are destroyed. Polymorphism allows objects of different types to respond to the same method call. The destructor is automatically called when an object is destroyed to release its memory. The derived class destructor calls the base class destructor to ensure that the base class memory is released.

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.

Function overloading can be used to achieve polymorphism, where a derived class method is called through a base class pointer and the compiler selects the overloaded version based on the actual parameter types. In the example, the Animal class defines a virtual makeSound() function, and the Dog and Cat classes rewrite this function. When makeSound() is called through the Animal* pointer, the compiler will call the corresponding rewritten version based on the pointed object type, thus achieving polymorphism. sex.

Interface: An implementationless contract interface defines a set of method signatures in Java but does not provide any concrete implementation. It acts as a contract that forces classes that implement the interface to implement its specified methods. The methods in the interface are abstract methods and have no method body. Code example: publicinterfaceAnimal{voideat();voidsleep();} Abstract class: Partially implemented blueprint An abstract class is a parent class that provides a partial implementation that can be inherited by its subclasses. Unlike interfaces, abstract classes can contain concrete implementations and abstract methods. Abstract methods are declared with the abstract keyword and must be overridden by subclasses. Code example: publicabstractcla

Advantages and Disadvantages of C++ Polymorphism: Advantages: Code Reusability: Common code can handle different object types. Extensibility: Easily add new classes without modifying existing code. Flexibility and maintainability: separation of behavior and type improves code flexibility. Disadvantages: Runtime overhead: Virtual function dispatch leads to increased overhead. Code Complexity: Multiple inheritance hierarchies add complexity. Binary size: Virtual function usage increases binary file size. Practical case: In the animal class hierarchy, polymorphism enables different animal objects to make sounds through Animal pointers.
