PHP4之真OO
本文的作者Johan Persson是PHP中著名的JpGraph图表类库的开发者. 本文是作者对于在PHP4中进行面向对象开发时需要注意的几个小问题的总结.
翻译: Binzy Wu [Mail: Binzy at JustDN dot COM], 水平有限, 欢迎探讨. 2004-2-4
简介
本文的对象是那些曾使用更加成熟的OO [1] 语言, 如Eiffel, Java, C# [2] or C++(), 进行开发的朋友(如我自己). 在使用PHP4进行完全的OO开发时有着许多的语义[3] (semantic)
上的陷阱[4].
希本文内容可助人避我曾犯之错.
引用 VS 拷贝语义
这基本上是错误的主要来源(至少对于我来说).即使在PHP的文档中你可以读到PHP4较之引用更多使用拷贝语义(如其他我所知的面向对象语言), 但这仍将使你最后在一些细小之处困扰.
接下来的两部分用于阐述二个小的例子, 在这二个例子中拷贝语义也许会令你惊讶.
要时刻牢记重要的是一个类的变量不是一个指向类的指针而是实际的类自己本身[5]. 大多数问题引发自对于赋值操作符(=)的误解, 即以为是给一个对象一个别名, 而实际上却是一个新的拷贝. 例如假设$myObj是某个类的实例, 并且它有一个Set()方法. 那么下面的代码也许不会像一个C++(或者Java)程序员所期望的那样工作.
function SomeFunction($aObj) { $aObj->Set(10); }
…
SomeFunction ($myObj);
…
那么现在, 很容易便会认为该函数所调用的Set()方法会作用于$myObj. 但这是错的!
其实发生的是$myObj被拷贝为一个新的, 与原对象一样的拷贝----参数$aObj. 然后当Set()方法被调用时, 它仅仅作用于本地拷贝而非原参数----$myObj.
在包含直接或间接(如上)赋值操作的地方就会发生各种各样的上述问题.
为了函数能像你所期望的那样行动(也许是), 那么你不得不通过修改方法申明来告诉PHP使用引用来传递对象, 如:
Function SomeFunction(&$aObj)
如果你再一次尝试上面的代码, 那么你会发现Set()方法将作用于原来的参数上, 因为现在我们在作用中创建了一个$myObj的别名----$aObj.
但是你不得不小心, 因为即使是&操作符也不是在任何时候都能救你, 如下面的举例.
从一个引用来获得引用
假设有如下代码:
$myObject = new SomeClass();$myRefToObject = &$myObject;
如果我们现在想要一个引用的拷贝(因某些理由), 那么我们要做什么呢? 你可能会由于$myRefToObject已经是引用而试图那么写:
$myCopyRefToObject = $myRefToObject;
正确么? 不! PHP会创建$myRefToObject所引用对象的新拷贝. 如果你想拷贝一个对象的引用, 你不得不这么写:
$myCopyRefToObject = &$myRefToObject;
在与前所述例子相当的C++的例子中, 便会创建一个引用的引用. 与其在PHP中不同. 这是一个经验丰富的C++程序员常会作的直觉假设相反的, 而这会是你的PHP程序中小BUG的来源.
请小心由此所产生的间接(传递参数)或直接的问题.
我个人所达成的结论, 即最好的避免这些语义陷阱的方法是总是用引用来传递对象或者对象赋值. 这不仅仅改进了运行速度(更少的数据拷贝), 而且可以对像我这样的老狗而言使语义更加可预测.
在构造函数中对$this使用引用
在一个对象的构造函数里初始化作为其他对象发现者(Observer[6])的对象是一个常见的模式. 下面几行代码便是一个示例:
class Bettery
{
function Bettery() {…};
function AddObserver($method, &$obj)
{
$this->obs[] = array($obj, &$method)
}
function Notify(){…}
}
class Display
{
function Display(&$batt)
{
$batt->AddObserver("BatteryNotify",$this);
}
function BatteryNotify() {…}
}
但是, 这并不会正常工作, 如果你是这么实例化对象的:
$myBattery = new Battery();$myDisplay = new Display($myBattery);
这么做的错误在于new时在构造函数中使用$this并不会返回同一个对象. 反而会返回最近创建对象的一个拷贝. 即在调用AddObserver()时所传送的对象于原对象不是同一个. 然后当Battery类尝试通知所有它的观察者(Observer)(通过调用他们的Notify方法)时, 它并不会调用我们所创建的Display类而是$this所代表的类(即我们所创建的Display类的拷贝). 因此如果Notify()方法更新了一些实例变量, 并不像我们所设想原Display类会被更新, 因为更新的其实是个拷贝. 为了让它工作, 你必须使构造函数返回同一个对象, 正如与最初$this所象征的那样. 可以通过添加&符号于Display的构造, 如$myDisplay = & new Display($myBattery);
一个直接的结果是任何Display类的Client必须了解Display的实现细节. 事实上, 这会产生一个可能引起争论的问题: 所有对象的构建必须使用额外的&符号. 就我所说的基本上是安全的, 但忽略它可能会在某些时候得到不想要的如上述示例般的作用.
在JpGraph中使用了另一种方法来解决. 即需要使用通过添加一个能安全的使用&$this引用的”Init()”方法的所谓二阶段构造来”new”一个对象(仅仅是因为在构造函数中的$this引用返回对象的一个拷贝而不如所期望的那样执行). 因此上面的例子会如下实现:
$myBattery = new Battery();
$myDisplay = new Display();
$myDisplay->Init($myBattery);
如JPGraph.php中的”LinearScale”类.
使用foreach
另外一个相似代码却不同结果的问题是”foreach”结构的问题. 研究一下下面的二个循环结构的不同版本.
// Version 1
foreach( $this->plots as $p )
{
$p->Update();
}
…
// Version 2
for( $i=0; $i
{
$this->plots[$i]->Update();
}
现在是一个价值10美元的问题[7]: version1==version2么?
令人惊讶的答案是:No! 这是细小却是关键的不同. 在Version 1中, Update()方法将作用于”plots[]”数组中对象的副本. 因此数组中原来的对象并不会被更新.
在Version 2中Update()方法将如预期的作用于”plots[]”数组中的对象.
正如第一部分所陈述的, 这是PHP将对象实例作为对象本身来处理而非作为对象引用的结果.
译注:
[1]. OO: Object-Oriented, 面向对象.
[2]. 原文并无C#, 全因Binzy的个人爱好.
[3]. Semantic在本文中被译为”语义”, 如有任何建议请和Binzy联系.
[4]. C++中有一本著名的”C++ Gotchas”.
[5]. 这里的类应该是指Instance, 即实例.
[6]. 可参见”[GoF95]”, 即”Design Patterns”.
[7]. 有个挺有趣的关于交易的小故事:
有人用60美元买了一匹马, 又以70美元的价钱卖了出去;然后, 他又用80美元把它买回来, 最后以90美元的价钱卖出.在这桩马的交易中, 他? (A)赔了10美元; (B)收支平衡; ©赚了10美元;(D)赚了20美元; (E)赚了30美元.
这是美国密执安大学心理学家梅尔和伯克要大学生们计算的一个简单的算术题.结果只有不到40%的大学生能够作出正确答案, 多数人认为只赚了10美元.其实, 问题的条件十分明确, 这是两次交易, 每次都赚10美元, 而很多人却错误地认为当他用80美元买回来时己经亏损了10美元. 有趣的是, 同一问题, 以另一种方式提出来:有一个人用60美元买了一匹白马, 又以70元的值卖出去;然后, 用80美元买了一匹黑马, 又以90美元的值卖出去.在这桩买卖马的交易中, 他____(把同样的五个选择罗列出来).这时, 另一组大学生在回答上述问题时, 结果大家都答对了.

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

Fujifilm fans were recently very excited at the prospect of the X-T50, since it presented a relaunch of the budget-oriented Fujifilm X-T30 II that had become quite popular in the sub-$1,000 APS-C category. Unfortunately, as the Fujifilm X-T50's launc

We frequently report on devices based on displays with electronic ink, such as e-readers. The technology offers a number of advantages: it can be read in bright environments without a backlight, and it only requires power when switching without light

Function means function. It is a reusable code block with specific functions. It is one of the basic components of a program. It can accept input parameters, perform specific operations, and return results. Its purpose is to encapsulate a reusable block of code. code to improve code reusability and maintainability.

Samsung Smart Display M8 vs. Apple Studio Display: Design and Size Since its launch, the Apple Studio Display has been compared to the iMac, consisting of a relatively thin panel on a relatively simple L-shaped stand. It's a well-known and well-loved aesthetic, and Samsung seems to have borrowed it for its presentation. The Samsung SmartMonitor M8 uses the same idea of a thin screen on a stand that looks very similar. Some minor elements are different, such as the small section in the lower left corner that sticks out a bit and the Samsung's chin is very thin, but they seem to be close in terms of basic design. Samsung seems to have taken a lot of inspiration from the 24-inch iMac. Apple's display is smaller than Samsung's

Apple Studio Display is now officially available in stores, and many customers around the world have purchased the product. Unlike the ProDisplayXDR, the StudioDisplay has a unique power connector that appears to be non-removable. It turns out that the cable is removable, but you'll need a special tool to remove it. Apple says on its website that the Studio Display's power cord is not detachable -- and many users think so. That's because removing the cable with your hands seems impossible, but luckily the cable can be detached from the monitor. , Apple has a special tool for extracting information from its new StudioDispl

StudioDisplay and LG UltraFine5KDisplay occupy similar positions in the market, but Apple's monitor is $300 more expensive. Here's everything you need to know about how these monitors compare. Six years is a long time in the tech world, and it's also the time since Apple has sold a branded monitor that costs less than $5,000. During this time, Apple partnered with LG to sell the LG UltraFine series, which catered specifically to Mac users. In 2019, Apple stopped selling these LG monitors in favor of ProDisplayXDR, an affordable Mac-friendly display

Display usually refers to the operation or function of displaying data, information or results to the user in some way or outputting it to a screen or other device. Specific meaning: 1. In the command line interface (CLI), display may refer to outputting data in text, tables or other formats to the terminal window for users to view or analyze; 2. In the graphical user interface (GUI), Display may refer to displaying images, text, charts and other content on the application window or interface for user interaction or browsing, etc.

IntelMac users running Windows on a Mac can now update their drivers in BootCamp to support Apple's StudioDisplay. Apple regularly updates BootCamp to introduce support for new hardware, as well as typical compatibility and performance improvements. In the March software update, Apple has enabled BootCamp to work with the new StudioDisplay. The update that brings BootCamp to version 6.1.17 introduces two key support elements. First, it adds compatibility with StudioDisplay, ensuring
