Whats New in PHP 5 countstars(翻譯)
Jun 21, 2016 am 09:15 AM翻譯:深空
作者:Andi Gutmans, Stig Bakken, and Derick Rethans
不得擅自轉(zhuǎn)載。
Introduction [緒論]
Language Features [語言特性]
? New Object Oriented model [新的面向?qū)ο竽P蚞
? New Object Oriented Features [新的面向?qū)ο筇匦註
? Other New Language Features [其他新的語言特性]
General PHP changes [PHP 變化概要]
? XML and Web Services [XML 和 Web 服務(wù)器]
? New MySQLi (MySQL Improved) extension [新的 MySQLi (改良的 MySQL) 擴(kuò)展
]
? SQLite extension [SQLite 擴(kuò)展]
? Tidy extension [Tidy 擴(kuò)展]
? Perl extension [Perl 擴(kuò)展]
? Other New Things in PHP 5 [其他 PHP5 新事物]
Summary [總結(jié)]
The best way to be ready for the future is to invent it. (John Sculley)
為未來作好準(zhǔn)備的最好方法就是創(chuàng)造它。 (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.
只有時(shí)間能夠證明 PHP5 是否和他的兩個(gè)前輩(PHP3 和 PHP4)一樣成功。新的語言特性和
改變的目的是為了消除 PHP 可能已經(jīng)具有的弱點(diǎn), 和證實(shí)它作為全球最優(yōu)秀的網(wǎng)頁腳本
語言所處的領(lǐng)先地位。
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 和它的新特性的詳細(xì)描述。然而,作為 PHP4 的親密好友的你都渴望
知道 PHP5 究竟做了哪些更新,那么,這一章就是為你準(zhǔn)備的。
The chapter will cover:
本章將包括:
The new language features [新的語言特性]
News concerning PHP extensions [關(guān)于 PHP 擴(kuò)展的新消息]
Other noteworthy changes [其他值得注意的改變]
Language Features [語言特性]
New Object Oriented model [新的面向?qū)ο竽P蚞
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.
當(dāng) Zeev Suraski 在 PHP3 時(shí)期添加面向?qū)ο笳Z法的時(shí)候,它被作為 "syntactic sugar
for accessing collections" 添加。該面向?qū)ο竽P鸵仓С掷^承和允許類(或?qū)ο螅┚酆?
方法和屬性,但沒有更多的特性了。當(dāng) Zeev 和 Andi 為 PHP4 重寫腳本引擎的時(shí)候,它
是一個(gè)完全新的引擎,運(yùn)行更快、更穩(wěn)定,和集合了更多的特性。然而,面向?qū)ο竽P褪?
次在 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.
雖然,對(duì)象模型已經(jīng)嚴(yán)重限制了它在世界范圍上的廣泛使用--常常是非常大型的 PHP 應(yīng)
用程序,但是這個(gè)給人印象深刻的面向?qū)ο蟪绦蛟O(shè)計(jì)范例的應(yīng)用和 PHP4 的缺點(diǎn)導(dǎo)致它成
為 PHP5 發(fā)布版本的主要焦點(diǎn)。
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 的局限性在哪里呢?最大的局限性(這導(dǎo)致更大的局限)是,事實(shí)上
對(duì)象的拷貝和原始對(duì)象一樣,因此這實(shí)際上影響了 PHP 的開發(fā)。當(dāng)你將一個(gè)變量(它指向
一個(gè)對(duì)象)賦值給另外一個(gè)變量的時(shí)候,這個(gè)對(duì)象的一個(gè)拷貝將被創(chuàng)建。在 PHP4 的應(yīng)用
程序里這樣做不僅對(duì)執(zhí)行產(chǎn)生影響,而且通常導(dǎo)致模糊的行為和錯(cuò)誤。因?yàn)樵S多開發(fā)者認(rèn)
為這兩個(gè)變量指向相同的對(duì)象(事實(shí)并不如此)。它們分別指向了這個(gè)對(duì)象的兩個(gè)拷貝,
改變其中一個(gè)并不能改變另外一個(gè)。
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" 。原因是我們通過值將對(duì)象 $person 傳遞給函數(shù)
changeName(),那么,$person 的一個(gè)拷貝被創(chuàng)建,而 changeName() 運(yùn)行了 $person 的
這個(gè)拷貝。
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.
這種行為并不直觀,很多開發(fā)者本來都以為它會(huì)像 Java 的方式一樣運(yùn)行。在 Java 里,
變量實(shí)際上保持了對(duì)象的一個(gè)句柄(或者稱指示器),因此它僅僅是作為一個(gè)句柄而不是
全部被拷貝出來
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 有兩種不同類型的用戶,一種知道這個(gè)問題而另一種卻不知道。后者通常沒有注意到
這個(gè)問題,這個(gè)問題是否存在對(duì)于他們寫的代碼來說沒有多大關(guān)系。的確,這類人中的一
些人,通常整夜追捕那些他們不能確定的莫名其妙的錯(cuò)誤。過去的團(tuán)隊(duì)通常通過給這個(gè)對(duì)
象注冊(cè)一個(gè)引用來處理這個(gè)問題。這將防止引擎創(chuàng)建對(duì)象的拷貝,但是有點(diǎn)頭痛的是代碼
中包含了眾多的 ‘&’ 標(biāo)簽。
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.
舊的對(duì)象模型不僅僅導(dǎo)致上面提及的問題,而且阻礙了在這個(gè)已經(jīng)存在的對(duì)象模型上實(shí)現(xiàn)
一些附加的特性。
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 中,重寫了對(duì)象模型的結(jié)構(gòu)使它以對(duì)象句柄運(yùn)行。除非你使用 clone 關(guān)鍵字確切
地克隆了一個(gè)對(duì)象否則你根本不能創(chuàng)建對(duì)象的拷貝。在 PHP5 中,不需要傳遞對(duì)象的引用
或者注冊(cè)對(duì)象的引用。
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).
注意:傳遞引用和注冊(cè)引用仍然被支持,萬一你要改變一個(gè)變量的內(nèi)容(不管是對(duì)象或者
其他類型)。
New Object Oriented Features [新的面向?qū)ο筇匦註
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.
新的面向?qū)ο筇匦詫?shí)在太多了,不能在這一章節(jié)里詳細(xì)闡述,面向?qū)ο笳Z言章節(jié)將詳細(xì)闡
述每一個(gè)特性。
The following is a list of the main new features:
下面是主要的新特性列表:
1. public/private/protected access modifiers for methods and properties
1. 為方法和屬性添加了 公共的/私有的/保護(hù)的 訪問權(quán)限修飾
Allows the use of common OO access modifiers to control access to methods and
properties.
允許使用公共的面向?qū)ο笤L問權(quán)限修飾來控制方法和屬性的使用權(quán)限。
class MyClass {
private $id = 18;
public function getId() {
return $this->id;
}
}
2. Unified constructor name __construct()
2. 統(tǒng)一的構(gòu)造器名: __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.
代替了使用類名作為類的構(gòu)造器的名稱,它現(xiàn)在應(yīng)該被聲明為 __construct(),創(chuàng)建它比
在類的層次上替換類更容易。
class MyClass {
function __construct() {
print "Inside constructor";
}
}
3. Object destructor support by defining a __destructor() method
3. 對(duì)象析構(gòu)函數(shù)通過定義 __destructor() 方法得到支持
Allows defining a destructor function that runs when an object is destroyed.
允許定義一個(gè)析構(gòu)函數(shù)當(dāng)對(duì)象崩潰的時(shí)候運(yùn)行。
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.
賦予類具有更多的相互關(guān)系。一個(gè)類只能從另外一個(gè)類中繼承但是可以實(shí)現(xiàn)更多的想要的
接口。
interface Display {
function display();
}
class Circle implements Display {
function display() {
print "Displaying circle ";
}
}
5. instanceof operator
5. 實(shí)例操作
Language level support for is-a relationship checking. The PHP 4 is_a() functi
on is now deprecated.
提供 is-a 語言級(jí)別的檢查支持,PHP4 中的 is_a() 函數(shù)現(xiàn)在不贊成使用。
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 關(guān)鍵字允許你標(biāo)記方法,使得繼承類不能載入它們。
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:
在聲明一個(gè) final 類之后它們不能被繼承,下面的例子將產(chǎn)生錯(cuò)誤:
final class FinalClass {
}
class BogusClass extends FinalClass {
}
8. Explicit object cloning
8. 明確的對(duì)象克隆
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).
為了復(fù)制一個(gè)對(duì)象你必須使用 clone 關(guān)鍵字。你可以聲明一個(gè) __clone() 方法,當(dāng)一個(gè)
類被拷貝的時(shí)候它將執(zhí)行(在原對(duì)象屬性被拷貝之后)。
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.
類定義現(xiàn)在包含了常量定義,并且通過類來引用它們。
class MyClass {
const SUCCESS = "Success";
const FAILURE = "Failure";
}
print MyClass::SUCCESS;
10. Static members
10. 靜態(tài)成員
Classes definitions can now include static members (properties), accessible vi
a the class. Common usage of static members is in the Singleton pattern.
類定義現(xiàn)在包含了靜態(tài)成員(屬性)的定義,并通過類訪問它們。通常靜態(tài)成員用于單獨(dú)
的部分。
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. 靜態(tài)方法
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.
你現(xiàn)在可以定義一個(gè)靜態(tài)的方法,并且可以在沒有對(duì)象的范圍內(nèi)被調(diào)用。靜態(tài)方法沒有定
義 $this 變量作為其他特定對(duì)象的限制。
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 聲明防止它們被實(shí)例化。然而,你可以繼承一個(gè)抽象類。
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 聲明,因此,從一個(gè)繼承類延續(xù)他們的定義。一個(gè)類如果包含一
個(gè)抽象方法必須用 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.
函數(shù)聲明可以包含類類星體是作為他們的參數(shù),如果一個(gè)函數(shù)被一個(gè)錯(cuò)誤的類調(diào)用將打印
一個(gè)錯(cuò)誤。
function expectsMyClass(MyClass $obj) {
}
15. Support for dereferencing objects which are returned from methods.
15. 支持從方法中返回非關(guān)聯(lián)對(duì)象
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 中,你不能直接從一個(gè)方法中返回的對(duì)象中解析,你必須將這個(gè)對(duì)象注冊(cè)給一個(gè)
中間變量,然后解析它。
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 擴(kuò)展類執(zhí)行一個(gè)迭代接口。一旦你執(zhí)行這個(gè)接口,已將可以通過
使用 foreach() 語言結(jié)構(gòu)反復(fù)實(shí)例化這個(gè)類。
$obj = new MyIteratorImplementation();
foreach ($obj as $value) {
print "$value";
}
For a more complete example, please refer to the "Advanced OOP & Design Patter
ns" chapter.
查看一個(gè)更為完整的例子,請(qǐng)查閱“高級(jí)面向?qū)ο蠛驮O(shè)計(jì)模型”這一章節(jié)。
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.
許多開發(fā)人員在寫一個(gè)面向?qū)ο髴?yīng)用程序的時(shí)候,為每個(gè)類獎(jiǎng)勵(lì)以俄國 PHP 文件,其中一
個(gè)最大的煩惱是,在每一個(gè)腳本的開始部分都要寫一個(gè)很長(zhǎng)的文件包含列表(一個(gè)類包含
一次)。在 PHP5 中,這需要在這么做了。你可以定義一個(gè) __autoload() 函數(shù),它將自
動(dòng)調(diào)用一個(gè)還沒有被定義的類。通過調(diào)用這個(gè)函數(shù),腳本引擎將在 PHP 拋出一個(gè)錯(cuò)誤之前
一直持續(xù)嘗試讀取這個(gè)類。
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 添加了一個(gè)出名的 try/throw/catch 異常處理體系。僅允許你拋出一個(gè)繼承自一個(gè)
出現(xiàn)問題的類的對(duì)象。
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.
現(xiàn)在,為了向后兼容,絕大部分內(nèi)部函數(shù)不會(huì)拋出異常。然而,為了使用這些功能,新的
擴(kuò)展正在被制定,你可以在自己的代碼里使用它們。而且,和已經(jīng)存在的 set_error_han
dler() 類似,你可以在腳本終止前使用 set_exception_handler() 來捕獲一個(gè)未經(jīng)處理
過的異常。
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 里,你不能迭代一個(gè)數(shù)組和改變它們的值。PHP5 允許你通過使用‘&’(引用)
標(biāo)記 foreach() 循環(huán)來實(shí)現(xiàn),創(chuàng)建任何你改變的值影響你所迭代的數(shù)組。
foreach ($array as &$value) {
if ($value === "NULL") {
$value = NULL;
}
}
3. default values for by-reference parameters
3. 通過引用的參數(shù)的默認(rèn)值
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 中,參數(shù)只能通過值來接收默認(rèn)值?,F(xiàn)在給參數(shù)一個(gè)引用的值作為值得到支持。
function my_func(&$arg = null) {
if ($arg === NULL) {
print '$arg is empty';
}
}
my_func();
General PHP changes [PHP 變化概要]
XML and Web Services [XML 和 Web 服務(wù)器]
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 中的更新大概最為重要和激動(dòng)人心。PHP5 中增強(qiáng)的 XM
L 函數(shù)和相同領(lǐng)域里的其他 Web 技術(shù)一樣,而且超過它們中的一部分。
The Foundation [基礎(chǔ)]
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 擴(kuò)展使用 libxml2)得到支持;而 DOM 通過使用更加強(qiáng)
大的 GHOME 項(xiàng)目的 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 支持上優(yōu)于他人??删S護(hù)性差,新的 XML 標(biāo)準(zhǔn)不總
是被支持,性能不理想,和在這些擴(kuò)展之間沒有兼容性可言。
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 擴(kuò)展已經(jīng)被重寫并使用優(yōu)秀的 libxml2 XML 工具包(http://ww
w.xmlsoft.org/)。它具有非常豐富的特性。及高地保持和有效地實(shí)現(xiàn) XML 標(biāo)準(zhǔn)為 PHP 帶
來了 XML 的優(yōu)勢(shì)。
All the above mentioned extensions (SAX, DOM and XSLT) now use libxml2 includi
ng the new additional extensions SimpleXML and SOAP.
所有上面提及的這些擴(kuò)展 (SAX, DOM 和 XSLT) 現(xiàn)在使用 libxml2 --包含新的附加擴(kuò)展
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 的實(shí)現(xiàn)已經(jīng)從 Expat 轉(zhuǎn)到了 libxml2。雖然新的擴(kuò)展在兼容性方面可
能存在一些細(xì)微的差別。開發(fā)者仍然可以通過配置 PHP 來實(shí)現(xiàn) Expat 擴(kuò)展的支持(這并
不被推薦)。
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 的,但是它漏洞百出,存在著一個(gè)內(nèi)存漏
洞和由于多種原因 API 并不是依從于 W3C。DOM 擴(kuò)展為 PHP5 做了徹底的改進(jìn),不僅僅幾
乎完全重寫,而且現(xiàn)在也是 W3C 建議。比如,函數(shù)名現(xiàn)在通過 W3C 標(biāo)準(zhǔn)使用 studlyCap
s 作為描述,使得你在 PHP 中更容易閱讀總體的 W3C 文檔,和執(zhí)行你所學(xué)到的東西。
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 中可用。然而,在
大多數(shù)場(chǎng)合下,調(diào)整函數(shù)名到新的標(biāo)準(zhǔn)可能是個(gè)方法。
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 中,有兩個(gè)擴(kuò)展支持 XSL 轉(zhuǎn)化。第一個(gè)使用 Sablotron 擴(kuò)展而第二個(gè)在 DOM 擴(kuò)
展中使用 XSLT 支持。在 PHP5 中,寫了一個(gè)新的 XSL 擴(kuò)展,提一下,它是基于 libxml
2 擴(kuò)展的。由于在 PHP5 中,XSL 轉(zhuǎn)換不是提取 XSLT 樣式作為一個(gè)參數(shù)而是依靠 DOM 擴(kuò)
展去讀取它,這個(gè)樣式會(huì)被存儲(chǔ)到內(nèi)存中并應(yīng)用與許多文檔來節(jié)約執(zhí)行時(shí)間。
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.
大概,回顧過去一兩年,我們會(huì)很清晰地知道,SimpleXML 已經(jīng)改革了 PHP 開發(fā)者處理
XML 文件的方式。SimpleXML 能夠被真正的叫做"XML for Dummies"。代替 處理 DOM 甚至
更糟糕的 SAX,SimpleXML 將 XML 文件作為 PHP 對(duì)象來描述。你可以讀取、寫入或者使
用迭代方便的訪問元素和屬性。
Consider the following XML file:
考慮下面的 XML 文件:
The following piece of code prints each client’s name and account number:
下面的這段代碼打印每個(gè)顧客的名字和帳號(hào):
$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.
這個(gè)例子明顯體現(xiàn)了 SimpleXML 是多么的簡(jiǎn)單。
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.
萬一,你需要一些高級(jí)的特性作用于 SimpleXML 對(duì)象而它本身這種輕量級(jí)擴(kuò)展并不支持。
你可以通過調(diào)用 dom_import_simplexml() 將它轉(zhuǎn)化為一個(gè) DOM 樹,使用 DOM 操作它然
后使用 simplexml_import_dom() 轉(zhuǎn)回給 SimpleXML。多虧了這兩個(gè)擴(kuò)展使用相同的底層
XML 庫,在它們之間切換已經(jīng)實(shí)現(xiàn)。
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 作為實(shí)現(xiàn)并不能像使用內(nèi)置 C 擴(kuò)展一樣出色。其他可用的 C 擴(kuò)展不能達(dá)到穩(wěn)定性
而廣泛的應(yīng)用。因此 PHP5 的主配置沒有把它包含進(jìn)來。(這句好像有問題)
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 中完全作為一個(gè) C 擴(kuò)展重寫,雖然它僅僅在 BETA 版本后期中才完成
。由于它徹底的實(shí)現(xiàn) SOAP 的大部分標(biāo)準(zhǔn),它已經(jīng)被添加到默認(rèn)配置。
The following calls SomeFunction() defined in a WSDL file:
下面調(diào)用一個(gè) WSDL 文件中定義的 SomeFunction():
$client = new SoapClient("some.wsdl");
$client->SomeFunction($a, $b, $c);
New MySQLi (MySQL Improved) extension [新的 MySQLi (改良的 MySQL) 擴(kuò)展]
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/
已經(jīng)寫了一個(gè)新的 MySQL 擴(kuò)展,該擴(kuò)展允
許你使用 MySQL 4.1 或更新版本的新函數(shù)。與舊的 MySQL 擴(kuò)展相對(duì)比,新的擴(kuò)展提供給
你一個(gè)函數(shù)和一個(gè)面向?qū)ο蠼涌冢虼四憧梢愿鶕?jù)你的喜好來選擇。該擴(kuò)展支持新的特性
,包括預(yù)聲明和可變結(jié)合, SSL 和 連接壓縮, 處理控制, 復(fù)制支持和其他很多特性。
SQLite extension [SQLite 擴(kuò)展]
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 服務(wù)器而且非常適合不包含可伸縮性的 SQL 的應(yīng)用程序、或者你正在
使用不支持訪問 SQL 的 ISP。對(duì)比它的名字,暗示著 SQLite 是一個(gè)具有豐富特性和事務(wù)
支持、sub-selects、視圖和大型的 DB 文件。在這里將它作為 PHP5 的新特性提及,是因
為它在 PHP4 系列很后期才引入的,而且它通過提供一個(gè)面向?qū)ο蠼涌趤砝?PHP5 的優(yōu)
點(diǎn)和支持迭代。
Tidy extension [Tidy 擴(kuò)展]
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 包含一個(gè)很有用的 Tidy (http://tidy.sf.net/
庫支持。它允許 PHP 開發(fā)者分析
、診斷、清理和修復(fù) HTML 文檔。Tidy 擴(kuò)展支持函數(shù)和對(duì)象接口,而且它的 API 使用 P
HP5 的異常機(jī)制。
Perl extension [Perl 擴(kuò)展]
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.
雖然沒有綜合評(píng)價(jià)默認(rèn)的 PHP5 插件,Perl 擴(kuò)展允許你從 PHP 內(nèi)部調(diào)用 Perl 腳本、使
用 Perl 對(duì)象和使用其他 Perl 功能。該新擴(kuò)展位于 PECL (PHP 擴(kuò)展公共庫)倉庫中。
http://pecl.php.net/package/perl
Other New Things in PHP 5 [其他 PHP5 新事物]:
New memory manager
新的內(nèi)存管理
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 引擎特寫了一個(gè)新的內(nèi)存管理。兩個(gè)主要的優(yōu)點(diǎn)是更好的支持多線程環(huán)境(分配不需
要再做任何互斥鎖定),且在每個(gè)請(qǐng)求之后將更有效釋放分配的內(nèi)存單元。由于這是一個(gè)
根本的底層構(gòu)造改變,作為最終用戶你不會(huì)很直接注意到這個(gè)變化。
Dropped support for Windows 95
放棄對(duì) 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 平臺(tái)下運(yùn)行 PHP 將不再被支持。因?yàn)樗恢С?PHP 的功能。由于微軟已
經(jīng)在一年前正式停止對(duì) Windows 95 的支持,PHP 開發(fā)團(tuán)隊(duì)認(rèn)為這是一個(gè)英明的決策。
Summary [總結(jié)]
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 的大量改進(jìn)而感動(dòng)。由于初期的敘述,該章節(jié)沒有覆蓋所有的而僅僅是主
要的改進(jìn)。其他還包括輔助特性的改進(jìn),一系列錯(cuò)誤的修正和大部分的底層結(jié)構(gòu)的改進(jìn)。
接下來其它章節(jié)將覆蓋 PHP5 和給你一個(gè)新特性和其他遺漏的具體闡述。

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

The core method of building social sharing functions in PHP is to dynamically generate sharing links that meet the requirements of each platform. 1. First get the current page or specified URL and article information; 2. Use urlencode to encode the parameters; 3. Splice and generate sharing links according to the protocols of each platform; 4. Display links on the front end for users to click and share; 5. Dynamically generate OG tags on the page to optimize sharing content display; 6. Be sure to escape user input to prevent XSS attacks. This method does not require complex authentication, has low maintenance costs, and is suitable for most content sharing needs.

User voice input is captured and sent to the PHP backend through the MediaRecorder API of the front-end JavaScript; 2. PHP saves the audio as a temporary file and calls STTAPI (such as Google or Baidu voice recognition) to convert it into text; 3. PHP sends the text to an AI service (such as OpenAIGPT) to obtain intelligent reply; 4. PHP then calls TTSAPI (such as Baidu or Google voice synthesis) to convert the reply to a voice file; 5. PHP streams the voice file back to the front-end to play, completing interaction. The entire process is dominated by PHP to ensure seamless connection between all links.

To realize text error correction and syntax optimization with AI, you need to follow the following steps: 1. Select a suitable AI model or API, such as Baidu, Tencent API or open source NLP library; 2. Call the API through PHP's curl or Guzzle and process the return results; 3. Display error correction information in the application and allow users to choose whether to adopt it; 4. Use php-l and PHP_CodeSniffer for syntax detection and code optimization; 5. Continuously collect feedback and update the model or rules to improve the effect. When choosing AIAPI, focus on evaluating accuracy, response speed, price and support for PHP. Code optimization should follow PSR specifications, use cache reasonably, avoid circular queries, review code regularly, and use X

1. Maximizing the commercial value of the comment system requires combining native advertising precise delivery, user paid value-added services (such as uploading pictures, top-up comments), influence incentive mechanism based on comment quality, and compliance anonymous data insight monetization; 2. The audit strategy should adopt a combination of pre-audit dynamic keyword filtering and user reporting mechanisms, supplemented by comment quality rating to achieve content hierarchical exposure; 3. Anti-brushing requires the construction of multi-layer defense: reCAPTCHAv3 sensorless verification, Honeypot honeypot field recognition robot, IP and timestamp frequency limit prevents watering, and content pattern recognition marks suspicious comments, and continuously iterate to deal with attacks.

PHP does not directly perform AI image processing, but integrates through APIs, because it is good at web development rather than computing-intensive tasks. API integration can achieve professional division of labor, reduce costs, and improve efficiency; 2. Integrating key technologies include using Guzzle or cURL to send HTTP requests, JSON data encoding and decoding, API key security authentication, asynchronous queue processing time-consuming tasks, robust error handling and retry mechanism, image storage and display; 3. Common challenges include API cost out of control, uncontrollable generation results, poor user experience, security risks and difficult data management. The response strategies are setting user quotas and caches, providing propt guidance and multi-picture selection, asynchronous notifications and progress prompts, key environment variable storage and content audit, and cloud storage.

PHP ensures inventory deduction atomicity through database transactions and FORUPDATE row locks to prevent high concurrent overselling; 2. Multi-platform inventory consistency depends on centralized management and event-driven synchronization, combining API/Webhook notifications and message queues to ensure reliable data transmission; 3. The alarm mechanism should set low inventory, zero/negative inventory, unsalable sales, replenishment cycles and abnormal fluctuations strategies in different scenarios, and select DingTalk, SMS or Email Responsible Persons according to the urgency, and the alarm information must be complete and clear to achieve business adaptation and rapid response.

PHPisstillrelevantinmodernenterpriseenvironments.1.ModernPHP(7.xand8.x)offersperformancegains,stricttyping,JITcompilation,andmodernsyntax,makingitsuitableforlarge-scaleapplications.2.PHPintegrateseffectivelyinhybridarchitectures,servingasanAPIgateway

Select the appropriate AI voice recognition service and integrate PHPSDK; 2. Use PHP to call ffmpeg to convert recordings into API-required formats (such as wav); 3. Upload files to cloud storage and call API asynchronous recognition; 4. Analyze JSON results and organize text using NLP technology; 5. Generate Word or Markdown documents to complete the automation of meeting records. The entire process needs to ensure data encryption, access control and compliance to ensure privacy and security.
