探讨PHP5多态性的具体实现方式
PHP5作为一个新版本,实现了对迟绑定的支持,不过在实际应用中,迟绑定功能还存在一些问题,比如你使用的是更旧版本的PHP,那么你可能发现其中缺乏对于迟绑定的支持。我们今天为大家讲的就是关于一、 PHP 5和多态性
本文想讨论面向对象编程中最为重要的部分之一--多态性的设计。为了说明问题,我使用了PHP 5。在你继续阅读之前,请首先明确本文并不是完全有关于PHP的。尽管这种语言在以前的两个主要版本中在快速开发方面已经取得很大的进步,但是,在其与更为成熟的语言如C++或者Java相匹敌之前,它对于对象的支持还要经历一段历程。
如果你是一位面向对象编程的入门者,那么本文可能不适合你,因为PHP5多态性这部分知识比较特别:一旦理解了它,你将永远不会忘记。如果你想简单了解一点对象编程和设计知识,并且当某人说"某个对象是多态的"时,还不十分清楚这是什么意思的话,那么本文正适合你。
到本文最后,你应该知道什么是多态性以及如何把它应用到面向对象的设计中,并且你会了解PHP 5中对象编程的优点与不足。
二、什么是多态性?
多态性,其来自于dictionary.com的定义是"以不同形式,阶段或者类型出现在独立的组织中或者同种组织中,而不存在根本区别。"由该定义,我们可以认为,多态性是一种通过多种状态或阶段来描述相同对象的编程方式。其实,它的真正意义在于:实际开发中,我们只需要关注一个接口或基类的编程,而不必担心一个对象所属于的具体类(class)。
如果你熟悉设计模式,即使只是有个初步了解,那么你也会了解这个概念。事实上,PHP5多态性可能是基于模式设计编程中的最伟大的工具。它允许我们以一种逻辑的方式来组织相类似的对象从而实现在具体编码时不必担心对象的具体类型;而且,我们只需要对一个所期望的接口或基类编程即可。一个应用程序越抽象,则它就显得越灵活--而多态性是对行为加以抽象的最好的方式之一。
例如,让我们考虑一个叫Person的类。我们可以用称为David,Charles和Alejandro的类来子类化Person。Person有一个抽象方法AcceptFeedback(),所有的子类都要实现这个方法。这意味着,任何使用基类Person的子类的代码都能调用方法AcceptFeedback()。你不必检查该对象是一个David还是一个Alejandro,仅知道它是一个Person就够了。结果是,你的代码只需关注"最小公分母"-Person类即可。
在这个示例中的Person类也可以被创建为一个接口。当然,与上面相比存在一些区别,主要在于:一个接口并没有给出任何行为,而仅确定了一组规则。一个Person接口要求的是"你必须支持AddFeedback()方法",而一个Person类可以提供一些AddFeedback()方法的缺省代码-你对之的理解可以是"如果你不选择支持AddFeedback(),那么你应该提供一种缺省实现。"至于如何选择接口或基类则并非本文的主题;但是,一般说来,你需要通过基类来实现一个缺省的方法。如果你能够简单地勾勒出你的类所要实现的一组期望的功能,那么你也可以使用一个接口。
三、应用PHP5多态性设计
我们将继续使用Person基类的例子,现在让我们分析一个非多态性的实现。下列示例中使用了不同类型的Person对象--这是一种非常不理想的编程方式。注意,实际的Person类被省略。目前为止,我们仅关心代码调用的问题。
<ol class="dp-xml"> <li class="alt"><span><span><?php </span></span></li> <li class=""> <span> $</span><span class="attribute"><font color="#ff0000">name</font></span><span> = $_SESSION['name']; </span> </li> <li class="alt"> <span> $</span><span class="attribute"><font color="#ff0000">myPerson</font></span><span> = Person::GetPerson($name); </span> </li> <li class=""><span> switch (get_class($myPerson)){ </span></li> <li class="alt"><span>case 'David' : </span></li> <li class=""><span> $myPerson->AddFeedback('Great Article!','Some Reader', date('Y-m-d')); </span></li> <li class="alt"><span> break; </span></li> <li class=""><span>case 'Charles': </span></li> <li class="alt"><span> $myPerson->feedback[] = array('Some Reader', 'Great Editing!'); </span></li> <li class=""><span> break; </span></li> <li class="alt"><span>case 'Alejandro' : </span></li> <li class=""><span> $myPerson->Feedback->Append('Awesome JavaScript!'); </span></li> <li class="alt"><span> break; </span></li> <li class=""><span>default : </span></li> <li class="alt"><span> $myPerson->AddFeedback('Yay!'); </span></li> <li class=""><span> } </span></li> <li class="alt"><span>?> </span></li> </ol>
这个示例展示了行为不同的对象,还有一个switch语句用于区分不同的Person类对象,从而执行其各自相应的正确操作。注意,这里针对不同条件的回馈注释是不同的。在实际应用程序开发中可能不会出现这种情形;我仅为了简单地说明类实现中存在的区别。
下面的一个示例使用了PHP5多态性。
<ol class="dp-xml"> <li class="alt"><span><span><?php </span></span></li> <li class=""> <span> $</span><span class="attribute"><font color="#ff0000">name</font></span><span> = $_SESSION['name']; </span> </li> <li class="alt"> <span> $</span><span class="attribute"><font color="#ff0000">myPerson</font></span><span> = Person::GetPerson($name); </span> </li> <li class=""><span> $myPerson->AddFeedback('Great Article!', 'SomeReader', date('Y-m-d')); </span></li> <li class="alt"><span>?> </span></li> </ol>
注意,这里没有switch语句,而最重要的是,缺乏有关Person::GetPerson()会返回什么类型的对象。而另一个Person::AddFeedback()是一个多态方法。行为完全是由具体类进行封装的。请记住,在此无论我们使用的是David,Charles还是Alejandro,调用代码从不必了解具体类的功能,而仅知道基类就可以了。
尽管我的示例并不完美,但是,从调用代码的角度,它已经展示了PHP5多态性的基本用法。现在我们需要分析这些类的内部实现。从一个基类进行派生的一个最伟大的地方在于,该派生类能够存取父类的行为,这种情况常常是缺省的实现,但是也可能出现在类继承链中用于创建更为复杂的行为。下面是这种情况的一个简单展示。
<ol class="dp-xml"> <li class="alt"><span><span><?php </span></span></li> <li class=""><span>class Person{ </span></li> <li class="alt"><span> function AddFeedback($comment, $sender, $date){ </span></li> <li class=""><span>//把回馈添加到数据库 </span></li> <li class="alt"><span> } </span></li> <li class=""><span>} </span></li> <li class="alt"><span>class David extends Person{ </span></li> <li class=""><span> function AddFeedback($comment, $sender){ </span></li> <li class="alt"><span>parent::AddFeedback($comment, $sender, </span></li> <li class=""><span>date('Y-m-d')); </span></li> <li class="alt"><span> } </span></li> <li class=""><span>} </span></li> <li class="alt"><span>?> </span></li> </ol>
在此,David类中的AddFeedback方法实现中首先调用了Person::AddFeedback方法。你可能注意到,它模仿了C++,Java或C#中的方法重载。请记住,这仅是一个简单化的示例,并且你编写的PHP5多态性实际代码完全依赖于你的实际工程。

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

How to implement dual WeChat login on Huawei mobile phones? With the rise of social media, WeChat has become one of the indispensable communication tools in people's daily lives. However, many people may encounter a problem: logging into multiple WeChat accounts at the same time on the same mobile phone. For Huawei mobile phone users, it is not difficult to achieve dual WeChat login. This article will introduce how to achieve dual WeChat login on Huawei mobile phones. First of all, the EMUI system that comes with Huawei mobile phones provides a very convenient function - dual application opening. Through the application dual opening function, users can simultaneously

How to implement the WeChat clone function on Huawei mobile phones With the popularity of social software and people's increasing emphasis on privacy and security, the WeChat clone function has gradually become the focus of people's attention. The WeChat clone function can help users log in to multiple WeChat accounts on the same mobile phone at the same time, making it easier to manage and use. It is not difficult to implement the WeChat clone function on Huawei mobile phones. You only need to follow the following steps. Step 1: Make sure that the mobile phone system version and WeChat version meet the requirements. First, make sure that your Huawei mobile phone system version has been updated to the latest version, as well as the WeChat App.

"Zero Age" legend has it that a long time ago there was a miser named Pan God. He collected a large amount of treasures during his lifetime, but after his death these treasures also disappeared, leaving only a rumor that Pan God built a huge maze mausoleum. , hid the treasure inside, but unfortunately no one has found it yet. Only by gambling your life can you find those peerless treasures. Do you dare to bet on this game? If you find the treasure box, you may get equipment development props and a mysterious silver coin package. So where do you get the treasure map? You can get it from daily business missions. There are three types of treasure maps: Iceland treasure map, wilderness treasure map, and rainforest treasure map. You can dig out treasures by finding the treasure location marked on the treasure map. And digging for treasures will randomly encounter different events: there is a chance that the BOSS guarding the treasure will be alerted, and the BOSS will die.

The programming language PHP is a powerful tool for web development, capable of supporting a variety of different programming logics and algorithms. Among them, implementing the Fibonacci sequence is a common and classic programming problem. In this article, we will introduce how to use the PHP programming language to implement the Fibonacci sequence, and attach specific code examples. The Fibonacci sequence is a mathematical sequence defined as follows: the first and second elements of the sequence are 1, and starting from the third element, the value of each element is equal to the sum of the previous two elements. The first few elements of the sequence

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.

In today's software development field, Golang (Go language), as an efficient, concise and highly concurrency programming language, is increasingly favored by developers. Its rich standard library and efficient concurrency features make it a high-profile choice in the field of game development. This article will explore how to use Golang for game development and demonstrate its powerful possibilities through specific code examples. 1. Golang’s advantages in game development. As a statically typed language, Golang is used in building large-scale game systems.

The new version of "Onmyoji" "The Gods of Fortune Parade" will be launched on April 3, and the version event "The Gods of Fortune Parade" will also start! Come and slay the invading sea monsters, and embark on a journey to collect luck together - [Version Event" "Pour of the God of Fortune"] - Event time - After maintenance on April 3 - 23:59 on April 9 [Introduction to how to play] This event includes: exterminating the sea monster, anecdotes about returning from afar, collecting fortune, gift cards from the God of Fortune, and visiting stores Waiting for gameplay, Onmyoji with level ≥15 can click on the morning light Ebisu in the courtyard or the small paper man in the upper right corner of the courtyard to participate in the activity. - Defeat the Kraken - Consume "Taiyayaki" or "Stamina" to get event rewards. Chenhui Ebisu will also participate in the battle together! If you succeed in the challenge, you will receive rewards such as "Lucky Bubbles" and "Lucky God Bonds". As the number of levels of Kraken extermination increases, you can obtain

"Zero Era" is a 2.5D future fantasy massively multiplayer role-playing online game represented by Hip Hop Tribe Platform. It takes the post-apocalyptic world reconstruction as its theme and presents a mysterious new world where magic and technology coexist. It has steam technology, automatic Innovative gameplay such as creative skills fully demonstrates the steampunk culture full of endless changes and imagination. In addition to protecting characters and reducing damage encountered during adventures, equipment can also increase people's potential to the limit, allowing adventurers to exert extraordinary strength. For adventurers, having a special piece of equipment is even a symbol of identity and experience. Equipment is divided into 6 quality levels: common (white), good (green), excellent (blue), rare (purple), heroic (pink), and legendary (orange). ordinary(
