Whats New in PHP 5 countstars(翻译)
程序员文章站
2022-05-07 20:01:20
...
翻译:深空
作者:Andi Gutmans, Stig Bakken, and Derick Rethans
不得擅自转载。
Introduction [绪论]
Language Features [语言特性]
• New Object Oriented model [新的面向对象模型]
• New Object Oriented Features [新的面向对象特性]
• Other New Language Features [其他新的语言特性]
General PHP changes [PHP 变化概要]
• XML and Web Services [XML 和 Web 服务器]
• New MySQLi (MySQL Improved) extension [新的 MySQLi (改良的 MySQL) 扩展
]
• SQLite extension [SQLite 扩展]
• Tidy extension [Tidy 扩展]
• Perl extension [Perl 扩展]
• Other New Things in PHP 5 [其他 PHP5 新事物]
Summary [总结]
The best way to be ready for the future is to invent it. (John Sculley)
为未来作好准备的最好方法就是创造它。 (John Sculley)
Introduction [绪论]
Only time will tell if the PHP 5 release will be as successful as the releases
of its two predecessors (PHP 3 and PHP 4). The new features and changes aim t
o rid PHP of any weaknesses it may have had and make sure that it stays in the
lead as the best web scripting language on the globe.
只有时间能够证明 PHP5 是否和他的两个前辈(PHP3 和 PHP4)一样成功。新的语言特性和
改变的目的是为了消除 PHP 可能已经具有的弱点, 和证实它作为全球最优秀的网页脚本
语言所处的领先地位。
This book covers PHP 5 and its new features in great detail. However, for thos
e of you familiar with PHP 4, and are eager to know what is new in PHP 5, then
this chapter is for you.
这本书囊括了 PHP5 和它的新特性的详细描述。然而,作为 PHP4 的亲密好友的你都渴望
知道 PHP5 究竟做了哪些更新,那么,这一章就是为你准备的。
The chapter will cover:
本章将包括:
The new language features [新的语言特性]
News concerning PHP extensions [关于 PHP 扩展的新消息]
Other noteworthy changes [其他值得注意的改变]
Language Features [语言特性]
New Object Oriented model [新的面向对象模型]
When Zeev Suraski added the object-oriented syntax back in the days of PHP 3,
it was added as "syntactic sugar for accessing collections". The object-orient
ed model also had support for inheritance and allowed a class (and object) to
aggregate both methods and properties, but not much more. When Zeev and Andi r
ewrote the scripting engine for PHP 4, it was a completely new engine, running
much faster, much more stable and with many more features. However, the objec
t-oriented model first introduced in PHP 3, was barely touched.
当 Zeev Suraski 在 PHP3 时期添加面向对象语法的时候,它被作为 "syntactic sugar
for accessing collections" 添加。该面向对象模型也支持继承和允许类(或对象)聚合
方法和属性,但没有更多的特性了。当 Zeev 和 Andi 为 PHP4 重写脚本引擎的时候,它
是一个完全新的引擎,运行更快、更稳定,和集合了更多的特性。然而,面向对象模型首
次在 PHP3 中引入,却极少涉及。
Although the object model had serious limitations it was used extensively arou
nd the world, often in very large PHP applications. This impressive use of the
OOP paradigm with PHP 4 despite its weaknesses led to it being the main focus
for the PHP 5 release.
虽然,对象模型已经严重限制了它在世界范围上的广泛使用--常常是非常大型的 PHP 应
用程序,但是这个给人印象深刻的面向对象程序设计范例的应用和 PHP4 的缺点导致它成
为 PHP5 发布版本的主要焦点。
So what were some of the limitations in PHP 3 & 4? The biggest limitation (whi
ch led to further limitations) was the fact that the copy semantics of objects
were the same as for native types. So how did this actually affect the PHP de
veloper? When you’d assign a variable (that points to an object) to another v
ariable, a copy of the object would be created. Not only did this impact perfo
rmance but it usually also lead to obscure behavior and bugs in PHP 4 applicat
ions because many developers thought that both variables would be pointing at
the same object which wasn’t the case. They were pointing at separate copies
of the same object, changing one would not change the other.
那么,PHP3 & PHP4 的局限性在哪里呢?最大的局限性(这导致更大的局限)是,事实上
对象的拷贝和原始对象一样,因此这实际上影响了 PHP 的开发。当你将一个变量(它指向
一个对象)赋值给另外一个变量的时候,这个对象的一个拷贝将被创建。在 PHP4 的应用
程序里这样做不仅对执行产生影响,而且通常导致模糊的行为和错误。因为许多开发者认
为这两个变量指向相同的对象(事实并不如此)。它们分别指向了这个对象的两个拷贝,
改变其中一个并不能改变另外一个。
For example [比如]:
class Person {
var $name;
function getName() {
return $this->name;
}
function setName($name) {
$this->name = $name;
}
function Person($name) {
$this->setName($name);
}
}
function changeName($person, $name) {
$person->setName($name);
}
$person = new Person("Andi");
changeName($person, "Stig");
print $person->getName();
In PHP 4, this piece of code would print out "Andi". The reason is that we pas
s the object $person to the changeName() function by-value, and thus, $person
is copied and changeName() works on a copy of $person.
在 PHP4 中,这段代码将打印 "Andi" 。原因是我们通过值将对象 $person 传递给函数
changeName(),那么,$person 的一个拷贝被创建,而 changeName() 运行了 $person 的
这个拷贝。
This behavior is not very intuitive, as many developers would expect the Java-
like behavior. In Java variables actually hold a handle (or pointers) to the o
bject, and therefore, when it is copied only the handle and not the entire obj
ect is duplicated.
这种行为并不直观,很多开发者本来都以为它会像 Java 的方式一样运行。在 Java 里,
变量实际上保持了对象的一个句柄(或者称指示器),因此它仅仅是作为一个句柄而不是
全部被拷贝出来
There were two kinds of users in PHP 4, the ones who were aware of this proble
m and the ones who weren’t. The latter would usually not notice this problem
and their code was written in a way where it didn’t really matter if the prob
lem existed or not. Surely some of these people had sleepless nights trying to
track down weird bugs which they couldn’t pinpoint. The former group dealt w
ith this problem by always passing and assigning objects by reference. This wo
uld prevent the engine from copying their objects but would be quite a headach
e as the code included numerous ‘&’ signs.
PHP4 有两种不同类型的用户,一种知道这个问题而另一种却不知道。后者通常没有注意到
这个问题,这个问题是否存在对于他们写的代码来说没有多大关系。的确,这类人中的一
些人,通常整夜追捕那些他们不能确定的莫名其妙的错误。过去的团队通常通过给这个对
象注册一个引用来处理这个问题。这将防止引擎创建对象的拷贝,但是有点头痛的是代码
中包含了众多的 ‘&’ 标签。
The old object model not only led to the above-mentioned problems but also led
to fundamental problems that prevented implementing some additional features
on top of the existing object model.
旧的对象模型不仅仅导致上面提及的问题,而且阻碍了在这个已经存在的对象模型上实现
一些附加的特性。
In PHP 5, the infrastructure of the object model was rewritten to work with ob
ject handles. Unless you explicitly clone an object by using the clone keyword
you will never create behind the scene duplicates of your objects. In PHP 5,
there is neither a need to pass objects by reference nor assigning them by ref
erence.
在 PHP5 中,重写了对象模型的结构使它以对象句柄运行。除非你使用 clone 关键字确切
地克隆了一个对象否则你根本不能创建对象的拷贝。在 PHP5 中,不需要传递对象的引用
或者注册对象的引用。
Note: Passing by reference and assigning by reference is still supported, in c
ase you want to actually change a variable’s content (whether object or other
type).
注意:传递引用和注册引用仍然被支持,万一你要改变一个变量的内容(不管是对象或者
其他类型)。
New Object Oriented Features [新的面向对象特性]
The new object oriented features are too numerous to give a detailed descripti
on in this section. The object oriented language chapter goes over each featur
e in detail.
新的面向对象特性实在太多了,不能在这一章节里详细阐述,面向对象语言章节将详细阐
述每一个特性。
The following is a list of the main new features:
下面是主要的新特性列表:
1. public/private/protected access modifiers for methods and properties
1. 为方法和属性添加了 公共的/私有的/保护的 访问权限修饰
Allows the use of common OO access modifiers to control access to methods and
properties.
允许使用公共的面向对象访问权限修饰来控制方法和属性的使用权限。
class MyClass {
private $id = 18;
public function getId() {
return $this->id;
}
}
2. Unified constructor name __construct()
2. 统一的构造器名: __construct()
Instead of the constructor being the name of the class, it should now be decla
red as __construct(), making it easier to shift classes inside class hierarchi
es.
代替了使用类名作为类的构造器的名称,它现在应该被声明为 __construct(),创建它比
在类的层次上替换类更容易。
class MyClass {
function __construct() {
print "Inside constructor";
}
}
3. Object destructor support by defining a __destructor() method
3. 对象析构函数通过定义 __destructor() 方法得到支持
Allows defining a destructor function that runs when an object is destroyed.
允许定义一个析构函数当对象崩溃的时候运行。
class MyClass {
function __destruct() {
print "Destroying object";
}
}
?>
4. Interfaces
4. 接口
Gives the ability for a class to fulfill more than one is-a relationships. A c
lass can inherit from one class only but may implement as many interfaces as i
t wants.
赋予类具有更多的相互关系。一个类只能从另外一个类中继承但是可以实现更多的想要的
接口。
interface Display {
function display();
}
class Circle implements Display {
function display() {
print "Displaying circle ";
}
}
5. instanceof operator
5. 实例操作
Language level support for is-a relationship checking. The PHP 4 is_a() functi
on is now deprecated.
提供 is-a 语言级别的检查支持,PHP4 中的 is_a() 函数现在不赞成使用。
if ($obj instance of Circle) {
print '$obj is a Circle';
}
6. final methods
6. final 方法
The final keyword allows you to mark methods so that an inheriting class can't
overload them.
final 关键字允许你标记方法,使得继承类不能载入它们。
class MyClass {
final function getBaseClassName() {
return __CLASS__;
}
}
7. final classes
7. final 类
After declaring a class as final, it can’t be inherited. The following exampl
e would error out:
在声明一个 final 类之后它们不能被继承,下面的例子将产生错误:
final class FinalClass {
}
class BogusClass extends FinalClass {
}
8. Explicit object cloning
8. 明确的对象克隆
In order to clone an object you have to use the clone keyword. You may declare
a __clone() method which will be called during the clone process (after the p
roperties have been copied from the original object).
为了复制一个对象你必须使用 clone 关键字。你可以声明一个 __clone() 方法,当一个
类被拷贝的时候它将执行(在原对象属性被拷贝之后)。
class MyClass {
function __clone() {
print "Object is being cloned";
}
}
$obj = new MyClass();
clone $obj;
9. Class constants
9. 类常量
Classes definitions can now include constant values, and are referenced using
the class.
类定义现在包含了常量定义,并且通过类来引用它们。
class MyClass {
const SUCCESS = "Success";
const FAILURE = "Failure";
}
print MyClass::SUCCESS;
10. Static members
10. 静态成员
Classes definitions can now include static members (properties), accessible vi
a the class. Common usage of static members is in the Singleton pattern.
类定义现在包含了静态成员(属性)的定义,并通过类访问它们。通常静态成员用于单独
的部分。
class Singleton {
static private $instance = NULL;
private function __construct() {
}
static public function getInstance() {
if (self::$instance == NULL) {
self::$instance = new Singleton();
}
return self::$instance;
}
}
11. Static methods
11. 静态方法
You can now define methods as static allowing them to be called from non-objec
t context. Static methods don’t define the $this variable as they aren’t bou
nd to any specific object.
你现在可以定义一个静态的方法,并且可以在没有对象的范围内被调用。静态方法没有定
义 $this 变量作为其他特定对象的限制。
class MyClass {
static function helloWorld() {
print "Hello, world";
}
}
MyClass::helloWorld();
?>
12. abstract classes
12. 抽象类
A class may be declared as abstract so as to prevent it from being instantiate
d. However, you may inherit from an abstract class.
类可以使用 abstract 声明防止它们被实例化。然而,你可以继承一个抽象类。
abstract class MyBaseClass {
function display() {
print "Default display routine being called";
}
}
13. abstract methods
13. 抽象方法
A method may be declared as abstract, thereby deferring its definition to an i
nheriting class. A class that includes abstract methods must be declared as ab
stract.
方法可以使用 abstract 声明,因此,从一个继承类延续他们的定义。一个类如果包含一
个抽象方法必须用 abstract 声明。
abstract class MyBaseClass {
abstract function display();
}
14. Class type hints
14. 类类型提示
Function declarations may include class type hints for their parameters. If th
e functions are called with an incorrect class type an error occurs.
函数声明可以包含类类星体是作为他们的参数,如果一个函数被一个错误的类调用将打印
一个错误。
function expectsMyClass(MyClass $obj) {
}
15. Support for dereferencing objects which are returned from methods.
15. 支持从方法中返回非关联对象
In PHP 4, you could not directly dereference objects which are returned from m
ethods. You would have to first assign the object to a dummy variable and then
dereference it.
在 PHP4 中,你不能直接从一个方法中返回的对象中解析,你必须将这个对象注册给一个
中间变量,然后解析它。
PHP 4:
$dummy = $obj->method();
$dummy->method2();
PHP 5:
$obj->method()->method2();
16. Iterators
16. 迭代
PHP 5 allows both PHP classes and PHP extension classes to implement an Iterat
or interface. Once you implement this interface you will be able to iterate in
stances of the class by using the foreach() language construct.
PHP5 允许 PHP 类和 PHP 扩展类执行一个迭代接口。一旦你执行这个接口,已将可以通过
使用 foreach() 语言结构反复实例化这个类。
$obj = new MyIteratorImplementation();
foreach ($obj as $value) {
print "$value";
}
For a more complete example, please refer to the "Advanced OOP & Design Patter
ns" chapter.
查看一个更为完整的例子,请查阅“高级面向对象和设计模型”这一章节。
17. __autoload()
17. __autoload()
Many developers writing object-oriented applications create one PHP source fil
e per-class definition. One of the biggest annoyances is having to write a lon
g list of needed includes at the beginning of each script (one for each class)
. In PHP 5, this is no longer necessary. You may define an __autoload() functi
on which is automatically called in case you are trying to use a class which h
asn’t been defined yet. By calling this function the scripting engine is givi
ng a last chance to load the class before PHP bails out with an error.
许多开发人员在写一个面向对象应用程序的时候,为每个类奖励以俄国 PHP 文件,其中一
个最大的烦恼是,在每一个脚本的开始部分都要写一个很长的文件包含列表(一个类包含
一次)。在 PHP5 中,这需要在这么做了。你可以定义一个 __autoload() 函数,它将自
动调用一个还没有被定义的类。通过调用这个函数,脚本引擎将在 PHP 抛出一个错误之前
一直持续尝试读取这个类。
function __autoload($class_name) {
include_once($class_name . "php");
}
$obj = new MyClass1();
$obj2 = new MyClass2();
Other New Language Features [其他新的语言特性]
1. Exception handling
1. 异常处理
PHP 5 adds the ability for the well known try/throw/catch structured exception
handling paradigm. You are only allowed to throw objects which inherit from t
he Exception class.
PHP5 添加了一个出名的 try/throw/catch 异常处理体系。仅允许你抛出一个继承自一个
出现问题的类的对象。
class SQLException extends Exception {
public $problem;
function __construct($problem) {
$this->problem = $problem;
}
}
try {
...
throw new SQLException("Couldn’t connect to database");
...
} catch (SQLException $e) {
print "Caught an SQLException with problem $obj->problem";
} catch (Exception $e) {
print "Caught unrecognized exception";
}
Currently for backwards compatibility purposes most internal functions do not
throw exceptions. However, new extensions are making use of this capability an
d you can use it in your own source code. Also, similar to the already existin
g set_error_handler() you may use set_exception_handler() to catch an unhandle
d exception before the script terminates.
现在,为了向后兼容,绝大部分内部函数不会抛出异常。然而,为了使用这些功能,新的
扩展正在被制定,你可以在自己的代码里使用它们。而且,和已经存在的 set_error_han
dler() 类似,你可以在脚本终止前使用 set_exception_handler() 来捕获一个未经处理
过的异常。
2. foreach with references
2. foreach 与引用
In PHP 4, you could not iterate through an array and modify its values. PHP 5
supports this by allowing you to mark the foreach() loop with the ‘&’ (refer
ence) sign, thus, making any values you change affect the array you’re iterat
ing over.
在 PHP4 里,你不能迭代一个数组和改变它们的值。PHP5 允许你通过使用‘&’(引用)
标记 foreach() 循环来实现,创建任何你改变的值影响你所迭代的数组。
foreach ($array as &$value) {
if ($value === "NULL") {
$value = NULL;
}
}
3. default values for by-reference parameters
3. 通过引用的参数的默认值
In PHP 4, default values could only be given to parameters which are passed by
-value. Giving default values to by-reference parameters is now supported.
在 PHP4 中,参数只能通过值来接收默认值。现在给参数一个引用的值作为值得到支持。
function my_func(&$arg = null) {
if ($arg === NULL) {
print '$arg is empty';
}
}
my_func();
General PHP changes [PHP 变化概要]
XML and Web Services [XML 和 Web 服务器]
Following the changes in the language, the XML updates in PHP 5 are most proba
bly the most significant and exciting. The enhanced XML functionality in PHP 5
puts it on par with other web technologies in some areas and overtakes them i
n others.
随着语言中的改变,XML 在 PHP5 中的更新大概最为重要和激动人心。PHP5 中增强的 XM
L 函数和相同领域里的其他 Web 技术一样,而且超过它们中的一部分。
The Foundation [基础]
XML support in PHP 4 was implemented using a variety of underlying XML librari
es. SAX support was implemented using the old Expat library, XSLT was implemen
ted using the Sablotron library (or using libxml2 via the DOM extension) and D
OM was implemented using the more powerful libxml2 library by the GNOME projec
t.
XML 在 PHP4 中通过底层的 XML 库得到支持;SAX 通过旧的 Expat 库得到支持;XSLT 通
过 Sablotron 库(或者通过 DOM 扩展使用 libxml2)得到支持;而 DOM 通过使用更加强
大的 GHOME 项目的 libxml2 库得到支持。
Using a variety of libraries did not make PHP 4 excel when it came to XML supp
ort. Maintenance was poor, new XML standards weren’t always supported, perfor
mance wasn’t as good as it could have been, and interoperability between the
varies XML extensions did not exist.
使用这种种库并没有使 PHP4 在 XML 支持上优于他人。可维护性差,新的 XML 标准不总
是被支持,性能不理想,和在这些扩展之间没有兼容性可言。
In PHP 5, all XML extensions have been rewritten to use the superb libxml2 XML
toolkit (http://www.xmlsoft.org/
. It is a very feature rich, highly maintain
ed and efficient implementation of the XML standards bringing the cutting edge
of XML technology to PHP.
在 PHP5 中,所有的 XML 扩展已经被重写并使用优秀的 libxml2 XML 工具包(http://ww
w.xmlsoft.org/)。它具有非常丰富的特性。及高地保持和有效地实现 XML 标准为 PHP 带
来了 XML 的优势。
All the above mentioned extensions (SAX, DOM and XSLT) now use libxml2 includi
ng the new additional extensions SimpleXML and SOAP.
所有上面提及的这些扩展 (SAX, DOM 和 XSLT) 现在使用 libxml2 --包含新的附加扩展
SimpleXML 和 SOAP。
SAX
As mentioned, the new SAX implementation has switched from using Expat to libx
ml2. Although the new extension should be compatible there may be some small s
ubtle differences. Developers who still want to work with the Expat library ca
n do so by configuring and building PHP accordingly (not recommended).
提起它,新的 SAX 的实现已经从 Expat 转到了 libxml2。虽然新的扩展在兼容性方面可
能存在一些细微的差别。开发者仍然可以通过配置 PHP 来实现 Expat 扩展的支持(这并
不被推荐)。
DOM
Although DOM support in PHP 4 was also based on the libxml2 library, it was qu
ite buggy, had memory leaks and the API in many cases was not W3C compliant. T
he DOM extension went through a thorough facelift for PHP 5. Not only was the
extension mostly rewritten it is now also W3C complaint. For example, function
names now use studlyCaps as described by the W3C standard making it easier fo
r you to read general W3C documentation and implementing what you learnt, righ
t away in PHP. In addition, the DOM extension now supports three kinds of sche
mas for XML validation, DTD, XML Schema and RelaxNG.
虽然 DOM 支持在 PHP4 中仍然是基于 libxml2 的,但是它漏洞百出,存在着一个内存漏
洞和由于多种原因 API 并不是依从于 W3C。DOM 扩展为 PHP5 做了彻底的改进,不仅仅几
乎完全重写,而且现在也是 W3C 建议。比如,函数名现在通过 W3C 标准使用 studlyCap
s 作为描述,使得你在 PHP 中更容易阅读总体的 W3C 文档,和执行你所学到的东西。
As a result of these changes PHP 4 code using DOM will not always run in PHP 5
. However, in most cases adjusting the function names to the new standard will
probably do the trick.
由于这些改变的原因,PHP4 中使用 DOM 的代码将不可能总是在 PHP5 中可用。然而,在
大多数场合下,调整函数名到新的标准可能是个方法。
XSLT
In PHP 4, there were two extensions that supported XSL Transformations. The fi
rst was using the Sablotron extension and the second was using the XSLT suppor
t in the DOM extension. In PHP 5, a new XSL extension was written and, as ment
ioned, is based on the libxml2 extension. As in PHP 5, the XSL Transformation
does not take the XSLT stylesheet as a parameter but depends on the DOM extens
ion to load it, the stylesheet can be cached in memory and may be applied to m
any documents saving execution time
在 PHP4 中,有两个扩展支持 XSL 转化。第一个使用 Sablotron 扩展而第二个在 DOM 扩
展中使用 XSLT 支持。在 PHP5 中,写了一个新的 XSL 扩展,提一下,它是基于 libxml
2 扩展的。由于在 PHP5 中,XSL 转换不是提取 XSLT 样式作为一个参数而是依靠 DOM 扩
展去读取它,这个样式会被存储到内存中并应用与许多文档来节约执行时间。
SimpleXML
Probably when looking back in a year or two it will be clear that SimpleXML ha
s revolutionized the way PHP developers work with XML files. SimpleXML could r
eally be called "XML for Dummies". Instead of having to deal with DOM or even
worse SAX, SimpleXML represents your XML file as a native PHP object. You can
read, write or iterate over your XML file with ease accessing elements and att
ributes.
大概,回顾过去一两年,我们会很清晰地知道,SimpleXML 已经改革了 PHP 开发者处理
XML 文件的方式。SimpleXML 能够被真正的叫做"XML for Dummies"。代替 处理 DOM 甚至
更糟糕的 SAX,SimpleXML 将 XML 文件作为 PHP 对象来描述。你可以读取、写入或者使
用迭代方便的访问元素和属性。
Consider the following XML file:
考虑下面的 XML 文件:
John Doe
87234838
Janet Smith
72384329
The following piece of code prints each client’s name and account number:
下面的这段代码打印每个顾客的名字和帐号:
$clients = simplexml_load_file('clients.xml');
foreach ($clients->client as $client) {
print "$client->name has account number $client->account_number ";
}
It’s obvious how simple SimpleXML really is.
这个例子明显体现了 SimpleXML 是多么的简单。
And in case there is something advanced you need to do to your SimpleXML objec
t which isn’t supported in this lightweight extension, you can convert it to
a DOM tree by calling dom_import_simplexml(), manipulate it in DOM and covert
it back to SimpleXML using simplexml_import_dom(). Thanks to both extensions u
sing the same underlying XML library switching between these two has been made
a reality.
万一,你需要一些高级的特性作用于 SimpleXML 对象而它本身这种轻量级扩展并不支持。
你可以通过调用 dom_import_simplexml() 将它转化为一个 DOM 树,使用 DOM 操作它然
后使用 simplexml_import_dom() 转回给 SimpleXML。多亏了这两个扩展使用相同的底层
XML 库,在它们之间切换已经实现。
SOAP
Official native SOAP support in PHP 4 was lacking. The most commonly used SOAP
implementation was PEAR’s but as it was implemented entirely in PHP it could
not perform as well as a built-in C extension. Other available C extensions n
ever reached stability and wide adoption and, therefore, were not included in
the main PHP 5 distribution.
在 PHP4 中缺乏正式的 SOAP 支持。一般地使用 SOAP 是通过 PEAR 得到支持。但是,使
用 PHP 作为实现并不能像使用内置 C 扩展一样出色。其他可用的 C 扩展不能达到稳定性
而广泛的应用。因此 PHP5 的主配置没有把它包含进来。(这句好像有问题)
SOAP support in PHP 5 was completely rewritten as a C extension and, although
it was only completed at a very late stage in the beta process, it was incoope
rated into the default distribution due to its thorough implementation of most
of the SOAP standard.
SOAP 支持在 PHP5 中完全作为一个 C 扩展重写,虽然它仅仅在 BETA 版本后期中才完成
。由于它彻底的实现 SOAP 的大部分标准,它已经被添加到默认配置。
The following calls SomeFunction() defined in a WSDL file:
下面调用一个 WSDL 文件中定义的 SomeFunction():
$client = new SoapClient("some.wsdl");
$client->SomeFunction($a, $b, $c);
New MySQLi (MySQL Improved) extension [新的 MySQLi (改良的 MySQL) 扩展]
For PHP 5, MySQL AB (http://www.mysql.com/
has written a new MySQL extension
that allows you to take full advantage of the new functionality in MySQL 4.1 a
nd later. As opposed to the old MySQL extension, the new one gives you both a
functional and an object oriented interface so that you can choose what you pr
efer. New features supported by this extension include prepared statements and
variable binding, SSL and compressed connections, transaction control, replic
ation support and more...
为 PHP5,MySQL AB (http://www.mysql.com/
已经写了一个新的 MySQL 扩展,该扩展允
许你使用 MySQL 4.1 或更新版本的新函数。与旧的 MySQL 扩展相对比,新的扩展提供给
你一个函数和一个面向对象接口,因此你可以根据你的喜好来选择。该扩展支持新的特性
,包括预声明和可变结合, SSL 和 连接压缩, 处理控制, 复制支持和其他很多特性。
SQLite extension [SQLite 扩展]
Support for SQLite (http://www.sqlite.org/
was first introduced in the PHP 4.
3.x series. It is an embedded SQL library which does not require an SQL server
and is very suitable for applications which don’t require the scalability of
SQL servers or if you’re deploying at an ISP who doesn’t give you access to
an SQL server. Contrary to what its name implies SQLite is very feature rich
and supports transactions, sub-selects, views and large DB files. It is mentio
ned here as a PHP 5 feature because it was introduced so late in the PHP 4 ser
ies and as it takes advantage of PHP 5 by providing an object oriented interfa
ce and supporting iterators.
SQLite (http://www.sqlite.org/
支持首先于 PHP 4.3.x 引入。它是一种嵌入式的 SQ
L 库,不包含 SQL 服务器而且非常适合不包含可伸缩性的 SQL 的应用程序、或者你正在
使用不支持访问 SQL 的 ISP。对比它的名字,暗示着 SQLite 是一个具有丰富特性和事务
支持、sub-selects、视图和大型的 DB 文件。在这里将它作为 PHP5 的新特性提及,是因
为它在 PHP4 系列很后期才引入的,而且它通过提供一个面向对象接口来利用 PHP5 的优
点和支持迭代。
Tidy extension [Tidy 扩展]
PHP 5 includes support for the useful Tidy (http://tidy.sf.net/
library. It a
llows PHP developers to parse, diagnose, clean and repair HTML documents. The
Tidy extension supports both a functional and an object oriented interface, an
d it’s API uses the PHP 5 exception mechanism.
PHP5 包含一个很有用的 Tidy (http://tidy.sf.net/
库支持。它允许 PHP 开发者分析
、诊断、清理和修复 HTML 文档。Tidy 扩展支持函数和对象接口,而且它的 API 使用 P
HP5 的异常机制。
Perl extension [Perl 扩展]
Although not bundled in the default PHP 5 package, the Perl extension allows y
ou to call Perl scripts, use Perl objects and use other Perl functionality nat
ively from within PHP. This new extension sits within the PECL (PHP Extension
Community Library) repository a http://pecl.php.net/package/perl.
虽然没有综合评价默认的 PHP5 插件,Perl 扩展允许你从 PHP 内部调用 Perl 脚本、使
用 Perl 对象和使用其他 Perl 功能。该新扩展位于 PECL (PHP 扩展公共库)仓库中。
http://pecl.php.net/package/perl
Other New Things in PHP 5 [其他 PHP5 新事物]:
New memory manager
新的内存管理
The Zend Engine features a new memory manager. The two main advantages are bet
ter support for multi-threaded environments (allocations don’t need to do any
mutual exclusion locks) and after each request freeing the allocated memory b
locks is much more efficient. As this is an underlying infra-structure change
you will not notice it directly as the end-user.
Zend 引擎特写了一个新的内存管理。两个主要的优点是更好的支持多线程环境(分配不需
要再做任何互斥锁定),且在每个请求之后将更有效释放分配的内存单元。由于这是一个
根本的底层构造改变,作为最终用户你不会很直接注意到这个变化。
Dropped support for Windows 95
放弃对 Windows 95 的支持
Running PHP on the Windows 95 platform is not supported anymore due to it not
supporting functionality which PHP uses. As Microsoft has officially stopped s
upporting it over a year ago the PHP development community decided that this i
s a wise decision.
在 Windows 95 平台下运行 PHP 将不再被支持。因为它不支持 PHP 的功能。由于微软已
经在一年前正式停止对 Windows 95 的支持,PHP 开发团队认为这是一个英明的决策。
Summary [总结]
You must surely be impressed by the amount of improvements in PHP 5. As mentio
ned earlier, this chapter doesn’t cover all of the improvements but only the
main ones. Other improvements include additional features, a lot of bug fixes
and very much improved infrastructure. The following chapters will cover PHP 5
and will give you in-depth coverage of the mentioned new features and others
which were omitted.
你必定为 PHP5 的大量改进而感动。由于初期的叙述,该章节没有覆盖所有的而仅仅是主
要的改进。其他还包括辅助特性的改进,一系列错误的修正和大部分的底层结构的改进。
接下来其它章节将覆盖 PHP5 和给你一个新特性和其他遗漏的具体阐述。
作者:Andi Gutmans, Stig Bakken, and Derick Rethans
不得擅自转载。
Introduction [绪论]
Language Features [语言特性]
• New Object Oriented model [新的面向对象模型]
• New Object Oriented Features [新的面向对象特性]
• Other New Language Features [其他新的语言特性]
General PHP changes [PHP 变化概要]
• XML and Web Services [XML 和 Web 服务器]
• New MySQLi (MySQL Improved) extension [新的 MySQLi (改良的 MySQL) 扩展
]
• SQLite extension [SQLite 扩展]
• Tidy extension [Tidy 扩展]
• Perl extension [Perl 扩展]
• Other New Things in PHP 5 [其他 PHP5 新事物]
Summary [总结]
The best way to be ready for the future is to invent it. (John Sculley)
为未来作好准备的最好方法就是创造它。 (John Sculley)
Introduction [绪论]
Only time will tell if the PHP 5 release will be as successful as the releases
of its two predecessors (PHP 3 and PHP 4). The new features and changes aim t
o rid PHP of any weaknesses it may have had and make sure that it stays in the
lead as the best web scripting language on the globe.
只有时间能够证明 PHP5 是否和他的两个前辈(PHP3 和 PHP4)一样成功。新的语言特性和
改变的目的是为了消除 PHP 可能已经具有的弱点, 和证实它作为全球最优秀的网页脚本
语言所处的领先地位。
This book covers PHP 5 and its new features in great detail. However, for thos
e of you familiar with PHP 4, and are eager to know what is new in PHP 5, then
this chapter is for you.
这本书囊括了 PHP5 和它的新特性的详细描述。然而,作为 PHP4 的亲密好友的你都渴望
知道 PHP5 究竟做了哪些更新,那么,这一章就是为你准备的。
The chapter will cover:
本章将包括:
The new language features [新的语言特性]
News concerning PHP extensions [关于 PHP 扩展的新消息]
Other noteworthy changes [其他值得注意的改变]
Language Features [语言特性]
New Object Oriented model [新的面向对象模型]
When Zeev Suraski added the object-oriented syntax back in the days of PHP 3,
it was added as "syntactic sugar for accessing collections". The object-orient
ed model also had support for inheritance and allowed a class (and object) to
aggregate both methods and properties, but not much more. When Zeev and Andi r
ewrote the scripting engine for PHP 4, it was a completely new engine, running
much faster, much more stable and with many more features. However, the objec
t-oriented model first introduced in PHP 3, was barely touched.
当 Zeev Suraski 在 PHP3 时期添加面向对象语法的时候,它被作为 "syntactic sugar
for accessing collections" 添加。该面向对象模型也支持继承和允许类(或对象)聚合
方法和属性,但没有更多的特性了。当 Zeev 和 Andi 为 PHP4 重写脚本引擎的时候,它
是一个完全新的引擎,运行更快、更稳定,和集合了更多的特性。然而,面向对象模型首
次在 PHP3 中引入,却极少涉及。
Although the object model had serious limitations it was used extensively arou
nd the world, often in very large PHP applications. This impressive use of the
OOP paradigm with PHP 4 despite its weaknesses led to it being the main focus
for the PHP 5 release.
虽然,对象模型已经严重限制了它在世界范围上的广泛使用--常常是非常大型的 PHP 应
用程序,但是这个给人印象深刻的面向对象程序设计范例的应用和 PHP4 的缺点导致它成
为 PHP5 发布版本的主要焦点。
So what were some of the limitations in PHP 3 & 4? The biggest limitation (whi
ch led to further limitations) was the fact that the copy semantics of objects
were the same as for native types. So how did this actually affect the PHP de
veloper? When you’d assign a variable (that points to an object) to another v
ariable, a copy of the object would be created. Not only did this impact perfo
rmance but it usually also lead to obscure behavior and bugs in PHP 4 applicat
ions because many developers thought that both variables would be pointing at
the same object which wasn’t the case. They were pointing at separate copies
of the same object, changing one would not change the other.
那么,PHP3 & PHP4 的局限性在哪里呢?最大的局限性(这导致更大的局限)是,事实上
对象的拷贝和原始对象一样,因此这实际上影响了 PHP 的开发。当你将一个变量(它指向
一个对象)赋值给另外一个变量的时候,这个对象的一个拷贝将被创建。在 PHP4 的应用
程序里这样做不仅对执行产生影响,而且通常导致模糊的行为和错误。因为许多开发者认
为这两个变量指向相同的对象(事实并不如此)。它们分别指向了这个对象的两个拷贝,
改变其中一个并不能改变另外一个。
For example [比如]:
class Person {
var $name;
function getName() {
return $this->name;
}
function setName($name) {
$this->name = $name;
}
function Person($name) {
$this->setName($name);
}
}
function changeName($person, $name) {
$person->setName($name);
}
$person = new Person("Andi");
changeName($person, "Stig");
print $person->getName();
In PHP 4, this piece of code would print out "Andi". The reason is that we pas
s the object $person to the changeName() function by-value, and thus, $person
is copied and changeName() works on a copy of $person.
在 PHP4 中,这段代码将打印 "Andi" 。原因是我们通过值将对象 $person 传递给函数
changeName(),那么,$person 的一个拷贝被创建,而 changeName() 运行了 $person 的
这个拷贝。
This behavior is not very intuitive, as many developers would expect the Java-
like behavior. In Java variables actually hold a handle (or pointers) to the o
bject, and therefore, when it is copied only the handle and not the entire obj
ect is duplicated.
这种行为并不直观,很多开发者本来都以为它会像 Java 的方式一样运行。在 Java 里,
变量实际上保持了对象的一个句柄(或者称指示器),因此它仅仅是作为一个句柄而不是
全部被拷贝出来
There were two kinds of users in PHP 4, the ones who were aware of this proble
m and the ones who weren’t. The latter would usually not notice this problem
and their code was written in a way where it didn’t really matter if the prob
lem existed or not. Surely some of these people had sleepless nights trying to
track down weird bugs which they couldn’t pinpoint. The former group dealt w
ith this problem by always passing and assigning objects by reference. This wo
uld prevent the engine from copying their objects but would be quite a headach
e as the code included numerous ‘&’ signs.
PHP4 有两种不同类型的用户,一种知道这个问题而另一种却不知道。后者通常没有注意到
这个问题,这个问题是否存在对于他们写的代码来说没有多大关系。的确,这类人中的一
些人,通常整夜追捕那些他们不能确定的莫名其妙的错误。过去的团队通常通过给这个对
象注册一个引用来处理这个问题。这将防止引擎创建对象的拷贝,但是有点头痛的是代码
中包含了众多的 ‘&’ 标签。
The old object model not only led to the above-mentioned problems but also led
to fundamental problems that prevented implementing some additional features
on top of the existing object model.
旧的对象模型不仅仅导致上面提及的问题,而且阻碍了在这个已经存在的对象模型上实现
一些附加的特性。
In PHP 5, the infrastructure of the object model was rewritten to work with ob
ject handles. Unless you explicitly clone an object by using the clone keyword
you will never create behind the scene duplicates of your objects. In PHP 5,
there is neither a need to pass objects by reference nor assigning them by ref
erence.
在 PHP5 中,重写了对象模型的结构使它以对象句柄运行。除非你使用 clone 关键字确切
地克隆了一个对象否则你根本不能创建对象的拷贝。在 PHP5 中,不需要传递对象的引用
或者注册对象的引用。
Note: Passing by reference and assigning by reference is still supported, in c
ase you want to actually change a variable’s content (whether object or other
type).
注意:传递引用和注册引用仍然被支持,万一你要改变一个变量的内容(不管是对象或者
其他类型)。
New Object Oriented Features [新的面向对象特性]
The new object oriented features are too numerous to give a detailed descripti
on in this section. The object oriented language chapter goes over each featur
e in detail.
新的面向对象特性实在太多了,不能在这一章节里详细阐述,面向对象语言章节将详细阐
述每一个特性。
The following is a list of the main new features:
下面是主要的新特性列表:
1. public/private/protected access modifiers for methods and properties
1. 为方法和属性添加了 公共的/私有的/保护的 访问权限修饰
Allows the use of common OO access modifiers to control access to methods and
properties.
允许使用公共的面向对象访问权限修饰来控制方法和属性的使用权限。
class MyClass {
private $id = 18;
public function getId() {
return $this->id;
}
}
2. Unified constructor name __construct()
2. 统一的构造器名: __construct()
Instead of the constructor being the name of the class, it should now be decla
red as __construct(), making it easier to shift classes inside class hierarchi
es.
代替了使用类名作为类的构造器的名称,它现在应该被声明为 __construct(),创建它比
在类的层次上替换类更容易。
class MyClass {
function __construct() {
print "Inside constructor";
}
}
3. Object destructor support by defining a __destructor() method
3. 对象析构函数通过定义 __destructor() 方法得到支持
Allows defining a destructor function that runs when an object is destroyed.
允许定义一个析构函数当对象崩溃的时候运行。
class MyClass {
function __destruct() {
print "Destroying object";
}
}
?>
4. Interfaces
4. 接口
Gives the ability for a class to fulfill more than one is-a relationships. A c
lass can inherit from one class only but may implement as many interfaces as i
t wants.
赋予类具有更多的相互关系。一个类只能从另外一个类中继承但是可以实现更多的想要的
接口。
interface Display {
function display();
}
class Circle implements Display {
function display() {
print "Displaying circle ";
}
}
5. instanceof operator
5. 实例操作
Language level support for is-a relationship checking. The PHP 4 is_a() functi
on is now deprecated.
提供 is-a 语言级别的检查支持,PHP4 中的 is_a() 函数现在不赞成使用。
if ($obj instance of Circle) {
print '$obj is a Circle';
}
6. final methods
6. final 方法
The final keyword allows you to mark methods so that an inheriting class can't
overload them.
final 关键字允许你标记方法,使得继承类不能载入它们。
class MyClass {
final function getBaseClassName() {
return __CLASS__;
}
}
7. final classes
7. final 类
After declaring a class as final, it can’t be inherited. The following exampl
e would error out:
在声明一个 final 类之后它们不能被继承,下面的例子将产生错误:
final class FinalClass {
}
class BogusClass extends FinalClass {
}
8. Explicit object cloning
8. 明确的对象克隆
In order to clone an object you have to use the clone keyword. You may declare
a __clone() method which will be called during the clone process (after the p
roperties have been copied from the original object).
为了复制一个对象你必须使用 clone 关键字。你可以声明一个 __clone() 方法,当一个
类被拷贝的时候它将执行(在原对象属性被拷贝之后)。
class MyClass {
function __clone() {
print "Object is being cloned";
}
}
$obj = new MyClass();
clone $obj;
9. Class constants
9. 类常量
Classes definitions can now include constant values, and are referenced using
the class.
类定义现在包含了常量定义,并且通过类来引用它们。
class MyClass {
const SUCCESS = "Success";
const FAILURE = "Failure";
}
print MyClass::SUCCESS;
10. Static members
10. 静态成员
Classes definitions can now include static members (properties), accessible vi
a the class. Common usage of static members is in the Singleton pattern.
类定义现在包含了静态成员(属性)的定义,并通过类访问它们。通常静态成员用于单独
的部分。
class Singleton {
static private $instance = NULL;
private function __construct() {
}
static public function getInstance() {
if (self::$instance == NULL) {
self::$instance = new Singleton();
}
return self::$instance;
}
}
11. Static methods
11. 静态方法
You can now define methods as static allowing them to be called from non-objec
t context. Static methods don’t define the $this variable as they aren’t bou
nd to any specific object.
你现在可以定义一个静态的方法,并且可以在没有对象的范围内被调用。静态方法没有定
义 $this 变量作为其他特定对象的限制。
class MyClass {
static function helloWorld() {
print "Hello, world";
}
}
MyClass::helloWorld();
?>
12. abstract classes
12. 抽象类
A class may be declared as abstract so as to prevent it from being instantiate
d. However, you may inherit from an abstract class.
类可以使用 abstract 声明防止它们被实例化。然而,你可以继承一个抽象类。
abstract class MyBaseClass {
function display() {
print "Default display routine being called";
}
}
13. abstract methods
13. 抽象方法
A method may be declared as abstract, thereby deferring its definition to an i
nheriting class. A class that includes abstract methods must be declared as ab
stract.
方法可以使用 abstract 声明,因此,从一个继承类延续他们的定义。一个类如果包含一
个抽象方法必须用 abstract 声明。
abstract class MyBaseClass {
abstract function display();
}
14. Class type hints
14. 类类型提示
Function declarations may include class type hints for their parameters. If th
e functions are called with an incorrect class type an error occurs.
函数声明可以包含类类星体是作为他们的参数,如果一个函数被一个错误的类调用将打印
一个错误。
function expectsMyClass(MyClass $obj) {
}
15. Support for dereferencing objects which are returned from methods.
15. 支持从方法中返回非关联对象
In PHP 4, you could not directly dereference objects which are returned from m
ethods. You would have to first assign the object to a dummy variable and then
dereference it.
在 PHP4 中,你不能直接从一个方法中返回的对象中解析,你必须将这个对象注册给一个
中间变量,然后解析它。
PHP 4:
$dummy = $obj->method();
$dummy->method2();
PHP 5:
$obj->method()->method2();
16. Iterators
16. 迭代
PHP 5 allows both PHP classes and PHP extension classes to implement an Iterat
or interface. Once you implement this interface you will be able to iterate in
stances of the class by using the foreach() language construct.
PHP5 允许 PHP 类和 PHP 扩展类执行一个迭代接口。一旦你执行这个接口,已将可以通过
使用 foreach() 语言结构反复实例化这个类。
$obj = new MyIteratorImplementation();
foreach ($obj as $value) {
print "$value";
}
For a more complete example, please refer to the "Advanced OOP & Design Patter
ns" chapter.
查看一个更为完整的例子,请查阅“高级面向对象和设计模型”这一章节。
17. __autoload()
17. __autoload()
Many developers writing object-oriented applications create one PHP source fil
e per-class definition. One of the biggest annoyances is having to write a lon
g list of needed includes at the beginning of each script (one for each class)
. In PHP 5, this is no longer necessary. You may define an __autoload() functi
on which is automatically called in case you are trying to use a class which h
asn’t been defined yet. By calling this function the scripting engine is givi
ng a last chance to load the class before PHP bails out with an error.
许多开发人员在写一个面向对象应用程序的时候,为每个类奖励以俄国 PHP 文件,其中一
个最大的烦恼是,在每一个脚本的开始部分都要写一个很长的文件包含列表(一个类包含
一次)。在 PHP5 中,这需要在这么做了。你可以定义一个 __autoload() 函数,它将自
动调用一个还没有被定义的类。通过调用这个函数,脚本引擎将在 PHP 抛出一个错误之前
一直持续尝试读取这个类。
function __autoload($class_name) {
include_once($class_name . "php");
}
$obj = new MyClass1();
$obj2 = new MyClass2();
Other New Language Features [其他新的语言特性]
1. Exception handling
1. 异常处理
PHP 5 adds the ability for the well known try/throw/catch structured exception
handling paradigm. You are only allowed to throw objects which inherit from t
he Exception class.
PHP5 添加了一个出名的 try/throw/catch 异常处理体系。仅允许你抛出一个继承自一个
出现问题的类的对象。
class SQLException extends Exception {
public $problem;
function __construct($problem) {
$this->problem = $problem;
}
}
try {
...
throw new SQLException("Couldn’t connect to database");
...
} catch (SQLException $e) {
print "Caught an SQLException with problem $obj->problem";
} catch (Exception $e) {
print "Caught unrecognized exception";
}
Currently for backwards compatibility purposes most internal functions do not
throw exceptions. However, new extensions are making use of this capability an
d you can use it in your own source code. Also, similar to the already existin
g set_error_handler() you may use set_exception_handler() to catch an unhandle
d exception before the script terminates.
现在,为了向后兼容,绝大部分内部函数不会抛出异常。然而,为了使用这些功能,新的
扩展正在被制定,你可以在自己的代码里使用它们。而且,和已经存在的 set_error_han
dler() 类似,你可以在脚本终止前使用 set_exception_handler() 来捕获一个未经处理
过的异常。
2. foreach with references
2. foreach 与引用
In PHP 4, you could not iterate through an array and modify its values. PHP 5
supports this by allowing you to mark the foreach() loop with the ‘&’ (refer
ence) sign, thus, making any values you change affect the array you’re iterat
ing over.
在 PHP4 里,你不能迭代一个数组和改变它们的值。PHP5 允许你通过使用‘&’(引用)
标记 foreach() 循环来实现,创建任何你改变的值影响你所迭代的数组。
foreach ($array as &$value) {
if ($value === "NULL") {
$value = NULL;
}
}
3. default values for by-reference parameters
3. 通过引用的参数的默认值
In PHP 4, default values could only be given to parameters which are passed by
-value. Giving default values to by-reference parameters is now supported.
在 PHP4 中,参数只能通过值来接收默认值。现在给参数一个引用的值作为值得到支持。
function my_func(&$arg = null) {
if ($arg === NULL) {
print '$arg is empty';
}
}
my_func();
General PHP changes [PHP 变化概要]
XML and Web Services [XML 和 Web 服务器]
Following the changes in the language, the XML updates in PHP 5 are most proba
bly the most significant and exciting. The enhanced XML functionality in PHP 5
puts it on par with other web technologies in some areas and overtakes them i
n others.
随着语言中的改变,XML 在 PHP5 中的更新大概最为重要和激动人心。PHP5 中增强的 XM
L 函数和相同领域里的其他 Web 技术一样,而且超过它们中的一部分。
The Foundation [基础]
XML support in PHP 4 was implemented using a variety of underlying XML librari
es. SAX support was implemented using the old Expat library, XSLT was implemen
ted using the Sablotron library (or using libxml2 via the DOM extension) and D
OM was implemented using the more powerful libxml2 library by the GNOME projec
t.
XML 在 PHP4 中通过底层的 XML 库得到支持;SAX 通过旧的 Expat 库得到支持;XSLT 通
过 Sablotron 库(或者通过 DOM 扩展使用 libxml2)得到支持;而 DOM 通过使用更加强
大的 GHOME 项目的 libxml2 库得到支持。
Using a variety of libraries did not make PHP 4 excel when it came to XML supp
ort. Maintenance was poor, new XML standards weren’t always supported, perfor
mance wasn’t as good as it could have been, and interoperability between the
varies XML extensions did not exist.
使用这种种库并没有使 PHP4 在 XML 支持上优于他人。可维护性差,新的 XML 标准不总
是被支持,性能不理想,和在这些扩展之间没有兼容性可言。
In PHP 5, all XML extensions have been rewritten to use the superb libxml2 XML
toolkit (http://www.xmlsoft.org/
. It is a very feature rich, highly maintain
ed and efficient implementation of the XML standards bringing the cutting edge
of XML technology to PHP.
在 PHP5 中,所有的 XML 扩展已经被重写并使用优秀的 libxml2 XML 工具包(http://ww
w.xmlsoft.org/)。它具有非常丰富的特性。及高地保持和有效地实现 XML 标准为 PHP 带
来了 XML 的优势。
All the above mentioned extensions (SAX, DOM and XSLT) now use libxml2 includi
ng the new additional extensions SimpleXML and SOAP.
所有上面提及的这些扩展 (SAX, DOM 和 XSLT) 现在使用 libxml2 --包含新的附加扩展
SimpleXML 和 SOAP。
SAX
As mentioned, the new SAX implementation has switched from using Expat to libx
ml2. Although the new extension should be compatible there may be some small s
ubtle differences. Developers who still want to work with the Expat library ca
n do so by configuring and building PHP accordingly (not recommended).
提起它,新的 SAX 的实现已经从 Expat 转到了 libxml2。虽然新的扩展在兼容性方面可
能存在一些细微的差别。开发者仍然可以通过配置 PHP 来实现 Expat 扩展的支持(这并
不被推荐)。
DOM
Although DOM support in PHP 4 was also based on the libxml2 library, it was qu
ite buggy, had memory leaks and the API in many cases was not W3C compliant. T
he DOM extension went through a thorough facelift for PHP 5. Not only was the
extension mostly rewritten it is now also W3C complaint. For example, function
names now use studlyCaps as described by the W3C standard making it easier fo
r you to read general W3C documentation and implementing what you learnt, righ
t away in PHP. In addition, the DOM extension now supports three kinds of sche
mas for XML validation, DTD, XML Schema and RelaxNG.
虽然 DOM 支持在 PHP4 中仍然是基于 libxml2 的,但是它漏洞百出,存在着一个内存漏
洞和由于多种原因 API 并不是依从于 W3C。DOM 扩展为 PHP5 做了彻底的改进,不仅仅几
乎完全重写,而且现在也是 W3C 建议。比如,函数名现在通过 W3C 标准使用 studlyCap
s 作为描述,使得你在 PHP 中更容易阅读总体的 W3C 文档,和执行你所学到的东西。
As a result of these changes PHP 4 code using DOM will not always run in PHP 5
. However, in most cases adjusting the function names to the new standard will
probably do the trick.
由于这些改变的原因,PHP4 中使用 DOM 的代码将不可能总是在 PHP5 中可用。然而,在
大多数场合下,调整函数名到新的标准可能是个方法。
XSLT
In PHP 4, there were two extensions that supported XSL Transformations. The fi
rst was using the Sablotron extension and the second was using the XSLT suppor
t in the DOM extension. In PHP 5, a new XSL extension was written and, as ment
ioned, is based on the libxml2 extension. As in PHP 5, the XSL Transformation
does not take the XSLT stylesheet as a parameter but depends on the DOM extens
ion to load it, the stylesheet can be cached in memory and may be applied to m
any documents saving execution time
在 PHP4 中,有两个扩展支持 XSL 转化。第一个使用 Sablotron 扩展而第二个在 DOM 扩
展中使用 XSLT 支持。在 PHP5 中,写了一个新的 XSL 扩展,提一下,它是基于 libxml
2 扩展的。由于在 PHP5 中,XSL 转换不是提取 XSLT 样式作为一个参数而是依靠 DOM 扩
展去读取它,这个样式会被存储到内存中并应用与许多文档来节约执行时间。
SimpleXML
Probably when looking back in a year or two it will be clear that SimpleXML ha
s revolutionized the way PHP developers work with XML files. SimpleXML could r
eally be called "XML for Dummies". Instead of having to deal with DOM or even
worse SAX, SimpleXML represents your XML file as a native PHP object. You can
read, write or iterate over your XML file with ease accessing elements and att
ributes.
大概,回顾过去一两年,我们会很清晰地知道,SimpleXML 已经改革了 PHP 开发者处理
XML 文件的方式。SimpleXML 能够被真正的叫做"XML for Dummies"。代替 处理 DOM 甚至
更糟糕的 SAX,SimpleXML 将 XML 文件作为 PHP 对象来描述。你可以读取、写入或者使
用迭代方便的访问元素和属性。
Consider the following XML file:
考虑下面的 XML 文件:
The following piece of code prints each client’s name and account number:
下面的这段代码打印每个顾客的名字和帐号:
$clients = simplexml_load_file('clients.xml');
foreach ($clients->client as $client) {
print "$client->name has account number $client->account_number ";
}
It’s obvious how simple SimpleXML really is.
这个例子明显体现了 SimpleXML 是多么的简单。
And in case there is something advanced you need to do to your SimpleXML objec
t which isn’t supported in this lightweight extension, you can convert it to
a DOM tree by calling dom_import_simplexml(), manipulate it in DOM and covert
it back to SimpleXML using simplexml_import_dom(). Thanks to both extensions u
sing the same underlying XML library switching between these two has been made
a reality.
万一,你需要一些高级的特性作用于 SimpleXML 对象而它本身这种轻量级扩展并不支持。
你可以通过调用 dom_import_simplexml() 将它转化为一个 DOM 树,使用 DOM 操作它然
后使用 simplexml_import_dom() 转回给 SimpleXML。多亏了这两个扩展使用相同的底层
XML 库,在它们之间切换已经实现。
SOAP
Official native SOAP support in PHP 4 was lacking. The most commonly used SOAP
implementation was PEAR’s but as it was implemented entirely in PHP it could
not perform as well as a built-in C extension. Other available C extensions n
ever reached stability and wide adoption and, therefore, were not included in
the main PHP 5 distribution.
在 PHP4 中缺乏正式的 SOAP 支持。一般地使用 SOAP 是通过 PEAR 得到支持。但是,使
用 PHP 作为实现并不能像使用内置 C 扩展一样出色。其他可用的 C 扩展不能达到稳定性
而广泛的应用。因此 PHP5 的主配置没有把它包含进来。(这句好像有问题)
SOAP support in PHP 5 was completely rewritten as a C extension and, although
it was only completed at a very late stage in the beta process, it was incoope
rated into the default distribution due to its thorough implementation of most
of the SOAP standard.
SOAP 支持在 PHP5 中完全作为一个 C 扩展重写,虽然它仅仅在 BETA 版本后期中才完成
。由于它彻底的实现 SOAP 的大部分标准,它已经被添加到默认配置。
The following calls SomeFunction() defined in a WSDL file:
下面调用一个 WSDL 文件中定义的 SomeFunction():
$client = new SoapClient("some.wsdl");
$client->SomeFunction($a, $b, $c);
New MySQLi (MySQL Improved) extension [新的 MySQLi (改良的 MySQL) 扩展]
For PHP 5, MySQL AB (http://www.mysql.com/
has written a new MySQL extension
that allows you to take full advantage of the new functionality in MySQL 4.1 a
nd later. As opposed to the old MySQL extension, the new one gives you both a
functional and an object oriented interface so that you can choose what you pr
efer. New features supported by this extension include prepared statements and
variable binding, SSL and compressed connections, transaction control, replic
ation support and more...
为 PHP5,MySQL AB (http://www.mysql.com/
已经写了一个新的 MySQL 扩展,该扩展允
许你使用 MySQL 4.1 或更新版本的新函数。与旧的 MySQL 扩展相对比,新的扩展提供给
你一个函数和一个面向对象接口,因此你可以根据你的喜好来选择。该扩展支持新的特性
,包括预声明和可变结合, SSL 和 连接压缩, 处理控制, 复制支持和其他很多特性。
SQLite extension [SQLite 扩展]
Support for SQLite (http://www.sqlite.org/
was first introduced in the PHP 4.
3.x series. It is an embedded SQL library which does not require an SQL server
and is very suitable for applications which don’t require the scalability of
SQL servers or if you’re deploying at an ISP who doesn’t give you access to
an SQL server. Contrary to what its name implies SQLite is very feature rich
and supports transactions, sub-selects, views and large DB files. It is mentio
ned here as a PHP 5 feature because it was introduced so late in the PHP 4 ser
ies and as it takes advantage of PHP 5 by providing an object oriented interfa
ce and supporting iterators.
SQLite (http://www.sqlite.org/
支持首先于 PHP 4.3.x 引入。它是一种嵌入式的 SQ
L 库,不包含 SQL 服务器而且非常适合不包含可伸缩性的 SQL 的应用程序、或者你正在
使用不支持访问 SQL 的 ISP。对比它的名字,暗示着 SQLite 是一个具有丰富特性和事务
支持、sub-selects、视图和大型的 DB 文件。在这里将它作为 PHP5 的新特性提及,是因
为它在 PHP4 系列很后期才引入的,而且它通过提供一个面向对象接口来利用 PHP5 的优
点和支持迭代。
Tidy extension [Tidy 扩展]
PHP 5 includes support for the useful Tidy (http://tidy.sf.net/
library. It a
llows PHP developers to parse, diagnose, clean and repair HTML documents. The
Tidy extension supports both a functional and an object oriented interface, an
d it’s API uses the PHP 5 exception mechanism.
PHP5 包含一个很有用的 Tidy (http://tidy.sf.net/
库支持。它允许 PHP 开发者分析
、诊断、清理和修复 HTML 文档。Tidy 扩展支持函数和对象接口,而且它的 API 使用 P
HP5 的异常机制。
Perl extension [Perl 扩展]
Although not bundled in the default PHP 5 package, the Perl extension allows y
ou to call Perl scripts, use Perl objects and use other Perl functionality nat
ively from within PHP. This new extension sits within the PECL (PHP Extension
Community Library) repository a http://pecl.php.net/package/perl.
虽然没有综合评价默认的 PHP5 插件,Perl 扩展允许你从 PHP 内部调用 Perl 脚本、使
用 Perl 对象和使用其他 Perl 功能。该新扩展位于 PECL (PHP 扩展公共库)仓库中。
http://pecl.php.net/package/perl
Other New Things in PHP 5 [其他 PHP5 新事物]:
New memory manager
新的内存管理
The Zend Engine features a new memory manager. The two main advantages are bet
ter support for multi-threaded environments (allocations don’t need to do any
mutual exclusion locks) and after each request freeing the allocated memory b
locks is much more efficient. As this is an underlying infra-structure change
you will not notice it directly as the end-user.
Zend 引擎特写了一个新的内存管理。两个主要的优点是更好的支持多线程环境(分配不需
要再做任何互斥锁定),且在每个请求之后将更有效释放分配的内存单元。由于这是一个
根本的底层构造改变,作为最终用户你不会很直接注意到这个变化。
Dropped support for Windows 95
放弃对 Windows 95 的支持
Running PHP on the Windows 95 platform is not supported anymore due to it not
supporting functionality which PHP uses. As Microsoft has officially stopped s
upporting it over a year ago the PHP development community decided that this i
s a wise decision.
在 Windows 95 平台下运行 PHP 将不再被支持。因为它不支持 PHP 的功能。由于微软已
经在一年前正式停止对 Windows 95 的支持,PHP 开发团队认为这是一个英明的决策。
Summary [总结]
You must surely be impressed by the amount of improvements in PHP 5. As mentio
ned earlier, this chapter doesn’t cover all of the improvements but only the
main ones. Other improvements include additional features, a lot of bug fixes
and very much improved infrastructure. The following chapters will cover PHP 5
and will give you in-depth coverage of the mentioned new features and others
which were omitted.
你必定为 PHP5 的大量改进而感动。由于初期的叙述,该章节没有覆盖所有的而仅仅是主
要的改进。其他还包括辅助特性的改进,一系列错误的修正和大部分的底层结构的改进。
接下来其它章节将覆盖 PHP5 和给你一个新特性和其他遗漏的具体阐述。
上一篇: 利用PHPExcel转Excel饼图
下一篇: 这句正则表示的什么意思?
推荐阅读
-
[翻译][php扩展开发和嵌入式]第11章-php5对象
-
[翻译][php扩展开发和嵌入式]第5章-您的第一个扩展
-
交叉编译PHP5、Nginx、Squid的详细步骤 squid翻译 squid的注释 squid代
-
09-通过mod_php5支持PHP php5下载安装 php5英文翻译 php5官方下载
-
Whats New in PHP 5 countstars(翻译)
-
PHP手册翻译日记[5] chm 手册(附下载地址)
-
[翻译][php扩展开发和嵌入式]第5章-您的第一个扩展
-
09-通过mod_php5支持PHP php5下载安装 php5英文翻译 php5官方下载
-
交叉编译PHP5、Nginx、Squid的详细步骤 squid翻译 squid的注释 squid代
-
Whats New in PHP 5 countstars(翻译)