Ç°ÑÔ
½ñÌiͻȻÏeµ½PHP¹Ù½ÍøÕ¾ÉÏһת£¬Ò»Ñ۾Ϳ´µ½PHP5ÍƳoµÄͨ¸ae¡£ËaÈ»ÒÔÇ°¿´µ½¹ýPHP5µÄÔ¤¸ae£¬µ«»¹ÊÇ×Ðϸ¿´ÁËPHP 5/Zend Engine 2.0ÐÂÌØÐÔÒ»ÎÄ£¬Ò»¹ÉJAVAÆøÏ¢ÆËÃae¶øÀ´...
Ìؽ«¸ÃÎÄÊÔÒe³oÀ´£¬Ê×¢ÓÚCSDNÍøÕ¾£¬ÒÔ÷϶ÁÕß¡£
PHP 5/Zend Engine 2.0ÐÂÌØÐÔ
Ði»½´º Òe sfwebsite@hotmail.com
http://www.php.net/zend-engine-2.php
ȫеĶÔÏoÄ£ÐÍ
PHPÖеĶÔÏo´¦Ài²¿ÖÒÑÍeÈ«ÖØд£¬¾ßÓиu¼ÑµÄÐÔÄܺ͸u¶aµÄ¹¦ÄÜ¡£ÔÚPHPµÄÒÔÇ°°ae±¾ÖУ¬¶ÔÏoÓeÄÚ½¨±aÁ¿ÀaÐÍ£¨ÈçintegerºÍstring£(C)µÄ´¦Ài½¨Ïaͬ£¬Æa±×¶ËÊǵ±±aÁ¿±»¸³ÖµÎª¶ÔÏo»o¶ÔÏo×÷Ϊ²ÎÊý´«µÝʱ£¬µÃµ½µÄÊǶÔÏo¸´ÖÆÆ¡£¶øÔÚаae±¾ÖУ¬¶ÔÏoͨ¹ý¾a±u½øÐÐÒýÓ㬶ø²»ÊÇͨ¹ýËuµÄÖµ¡££¨¾a±u¿ÉÒÔÈÏÊÇΪÊǶÔÏoµÄ±eʶu£(C)
ºÜ¶aPHP³ÌÐoÔ±¿ÉÄÜδÒaʶµ½ÒÔÇ°µÄ¶ÔÏoÄ£Ð͵ġ°¸´ÖƹÖñ±¡±£¬Òo´ËÒÔÇ°µÄPHP³ÌÐo½«²»ÐeÒª×oÈκθu¸Ä£¬»oÖ»×oºÜСµÄ¸Ä¶¯¼´¿ÉÔËÐÐ
˽Óкͱ£»¤³ÉÔ±
PHP 5ÒýÈeÁË˽Óкͱ£»¤³ÉÔ±±aÁ¿£¬ËuÃÇ¿ÉÒÔ¶¨ÒaÀaÊoÐÔÔÚºÎʱ¿ÉÒÔ±»ÃÎÊ¡£
Àý
ÀaµÄ±£»¤³ÉÔ±±aÁ¿ÄÜÔÚ¸ÃÀaµÄÀ(C)Õ¹ÀaÖб»ÃÎÊ£¬¶ø˽ÓгÉÔ±±aÁ¿Ö»ÄÜÔÚ±¾ÀaÖб»ÃÎÊ¡£
<?php
class MyClass {
private $Hello = "Hello, World!\n";
protected $Bar = "Hello, Foo!\n";
protected $Foo = "Hello, Bar!\n";
function printHello() {
print "MyClass::printHello() " . $this->Hello;
print "MyClass::printHello() " . $this->Bar;
print "MyClass::printHello() " . $this->Foo;
}
}
class MyClass2 extends MyClass {
protected $Foo;
function printHello() {
MyClass::printHello(); /* Should print */
print "MyClass2::printHello() " . $this->Hello; /* Shouldn't print out anything */
print "MyClass2::printHello() " . $this->Bar; /* Shouldn't print (not declared)*/
print "MyClass2::printHello() " . $this->Foo; /* Should print */
}
}
$obj = new MyClass();
print $obj->Hello; / Shouldn't print out anything /
print $obj->Bar; / Shouldn't print out anything /
print $obj->Foo; / Shouldn't print out anything /
$obj->printHello(); / Should print /
$obj = new MyClass2();
print $obj->Hello; / Shouldn't print out anything /
print $obj->Bar; / Shouldn't print out anything /
print $obj->Foo; / Shouldn't print out anything /
$obj->printHello();
?>
˽Óкͱ£»¤½¨
ÔÚPHP 5(ZENDÒýÇae2)ÖУ¬»¹ÒýÈeÁË˽Óкͱ£»¤½¨¡£
Àý£º
<?php
class Foo {
private function aPrivateMethod() {
echo "Foo::aPrivateMethod() called.\n";
}
protected function aProtectedMethod() {
echo "Foo::aProtectedMethod() called.\n";
$this->aPrivateMethod();
}
}
class Bar extends Foo {
public function aPublicMethod() {
echo "Bar::aPublicMethod() called.\n";
$this->aProtectedMethod();
}
}
$o = new Bar;
$o->aPublicMethod();
?>
ÒÔÇ°´uÂeÖеÄÓû§×Ô¶¨ÒaÀa»o½¨ÖÐËa䶨Òa"public," "protected" »o "private"µÈ¹Ø¼u×Ö£¬µ«ÎÞÐe±a¼¼´¿ÉÔËÐС£
³eÏoÀaºÍ½¨
PHP 5»¹ÒýÈeÁ˳eÏoÀaºÍ½¨¡£³eÏo½¨Ö»ÉuÃ÷½¨¶¨Òa£¬ ²»¹(C)ʵ¼ÊÔËÐС£°uº¬³eÏo½¨µÄÀaÐeÒªÉuÃ÷Ϊ³eÏoÀa¡£
Àý£º
<?php
abstract class AbstractClass {
abstract public function test();
}
class ImplementedClass extends AbstractClass {
public function test() {
echo "ImplementedClass::test() called.\n";
}
}
$o = new ImplementedClass;
$o->test();
?>
³eÏoÀa²»ÄÜʵÀý»¯¡£ÒÔÇ°´uÂeÖеÄÓû§×Ô¶¨ÒaÀa»o½¨ÖÐËa䶨Òa"abstract¡±¹Ø¼u×Ö£¬µ«ÎÞÐe±a¼¼´¿ÉÔËÐС£
½Ó¿Ú
ZENDÒýÇae2.0ÒýÈeÁ˽ӿڡ£Ò»¸oÀa¿ÉÒÔÔËÐÐÈÎÒaµÄ½Ó¿ÚÁбi¡£
Example
Àý£º
<?php
interface Throwable {
public function getMessage();
}
class Exception implements Throwable {
public function getMessage() {
// ...
}
?>
ÒÔÇ°´uÂeÖеÄÓû§×Ô¶¨ÒaÀa»o½¨ÖÐËa䶨Òa"interface¡±¹Ø¼u×Ö£¬µ«ÎÞÐe±a¼¼´¿ÉÔËÐС£
ÀaÀaÐͶ¨Òa
ÔÚ±£ÁoÀaÎÞÐe¶¨ÒaÀaÐ͵Äͬʱ£¬PHP 5ÒýÈeÁËÀaÀaÐͶ¨ÒaÀ´ÉuÃ÷Ï£Íu°ÑÄĸoÀaͨ¹ý²ÎÊý´«µÝ¸øÒ»¸o½¨¡£
Example
Àý£º
<?php
interface Foo {
function a(Foo $foo);
}
interface Bar {
function b(Bar $bar);
}
class FooBar implements Foo, Bar {
function a(Foo $foo) {
// ...
}
function b(Bar $bar) {
// ...
}
}
$a = new FooBar;
$b = new FooBar;
$a->a($b);
$a->b($b);
?>
ÕaÐ(C)ÀaÀaÐͶ¨ÒaÔÚ²»ÏoÒ»Ð(C)ÐeÒªÀaÐÍÔ¤¶¨ÒaµÄÓiÑÔÔÚ±aÒeÖнøÐмi²e£¬¶øÊÇÔÚÔËÐÐʱ½øÐС£ÕaÒaζ×Å£º
<?php
function foo(ClassName $object) {
// ...
}
?>
µÈ¼ÛÓÚ£º
<?php
function foo($object) {
if (!($object instanceof ClassName)) {
die("Argument 1 must be an instance of ClassName");
}
}
?>
±¾Ói*¨Ö»ÓÃÓÚ¶ÔÏo»oÀa£¬²»ÊÊÓÃÓÚÄÚ½¨ÀaÐÍ¡£
final
PHP 5ÒýÈeÁË¡°final¡±¹Ø¼u×Ö¶¨ÒaÔÚ×ÓÀaÖв»Äܱ»¸²¸ÇµÄ³ÉÔ±»o½¨¡£
Àý£º
<?php
class Foo {
final function bar() {
// ...
}
}
?>
ÒÔÇ°´uÂeÖеÄÓû§×Ô¶¨ÒaÀa»o½¨ÖÐËa䶨Òa"final"¹Ø¼u×Ö£¬µ«ÎÞÐe±a¼¼´¿ÉÔËÐС£
¶ÔÏo¿Ë¡
PHP 4ÔÚ¶ÔÏo±»¸´ÖÆʱ£¬Óû§²»Äܾo¶¨¿½±´µÄ»uÖÆ¡£ÔÚ¸´ÖÆʱ£¬PHP 4ֻһλһλµØ¸´ÖÆÒ»¸oºÍÔÀ´¶ÔÏoһģһÑuµÄ¸´ÖÆÆ¡£
ÎÒÃDz¢²»ÊÇÿ´Î¶¼Òª½¨Á¢Ò»¸oÍeÈ«Ò»ÑuµÄ¸´ÖÆÆ¡£Ò»¸oºÜºÃµÄÐeÒªÒ»ÖÖ¸´ÖÆ»uÖƵÄÀý×ÓÊÇ£¬µ±ÓÐÒ»¸o´u±iÒ»¸oGTK´°¿ÚµÄ¶ÔÏo£¬ËuÓµÓиô°¿ÚµÄËuÓÐ×ÊÔ´£¬µ±Äa½¨Á¢Ò»¸o¿½±´Ê±£¬Äa¿ÉÄÜÐeÒªÒ»¸oеĴ°¿Ú£¬ËuÓµÓÐÔ´°¿ÚµÄËuÓÐÊoÐÔ£¬µ«ÐeÒªÓµÓÐд°¿ÚµÄ×ÊÔ´¡£ÁiÍaÒ»¸oÀý×ÓÊÇÄaÓÐÒ»¸o¶ÔÏoÒýÓÃÁËÁiÍaÒ»¸o¶ÔÏo£¬µ±Äa¸´ÖƸ¸¶ÔÏoʱ£¬ÄaÏ£Íu½¨Á¢ÄǸoÒýÓöÔÏoµÄÐÂʵÀý£¬ÒÔʹ¸´ÖÆÆÒýÓÃËu¡£
¶ÔÒ»¸o¶ÔÏoµÄ¿½±´Í¨¹ýµ÷ÓöÔÏoµÄ__clone()½¨Íe³É£º
<?php
$copy_of_object = $object->__clone();
?>
µ±¿ª¢ÕßÇeÇo½¨Á¢Ò»¸o¶ÔÏoµÄеĿ½±´Ê±£¬ZENDÒýÇae»a¼i²eÊÇñ¶¨ÒaÁË__clone()½¨¡£Èç¹u䶨ÒaµÄ»°£¬Ëu»aµ÷ÓÃÒ»¸oĬÈϵÄ__clone()½¨À´¸´ÖƸöÔÏoµÄËuÓÐÊoÐÔ¡£Èç¹u¶¨ÒaÁ˸ý¨£¬¸Ã½¨»a¸ºÔðÔÚ¿½±´ÖÐÉeÖñØÒªµÄÊoÐÔ¡£Îª½±aÆð¼u£¬ÒýÇae»aÌa¹(C)Ò»¸oº¯Êý´ÓÔ´¶ÔÏoÖе¼ÈeËuÓеÄÊoÐÔ£¬ÕaÑuËu¾Í¿ÉÒÔÏȵõ½Ò»¸o¾ßÓÐÖµµÄÔ´¶ÔÏo¿½±´£¬Ö»ÐeÒª¶ÔÐeÒª¸Ä±aµÄÊoÐÔ½øÐи²¸Ç¼´¿É¡£
Àý£º
<?php
class MyCloneable {
static $id = 0;
function MyCloneable() {
$this->id = self::$id++;
}
function __clone() {
$this->name = $that->name;
$this->address = "New York";
$this->id = self::$id++;
}
}
$obj = new MyCloneable();
$obj->name = "Hello";
$obj->address = "Tel-Aviv";
print $obj->id . "\n";
$obj = $obj->__clone();
print $obj->id . "\n";
print $obj->name . "\n";
print $obj->address . "\n";
?>
ͳһµÄ¹¹Ôi½¨Ãu
ZENDÒýÇaeÔÊÐi¿ª¢Õ߶¨ÒaÀaµÄ¹¹Ôi½¨¡£¾ßÓй¹Ôi½¨µÄÀaÔÚн¨Ê±»aÊ×Ïȵ÷Óù¹Ôi½¨£¬¹¹Ôi½¨ÊÊÓÃÓÚÔÚÕýʽʹÓøÃÀaÇ°½øÐеijoʼ»¯¡£
ÔÚPHP4ÖУ¬¹¹Ôi½¨µÄÃu³ÆÓeÀaÃuÏaͬ¡£ÓÉÓÚÔÚÅÉÉuÀaÖе÷Óø¸ÀaµÄ×÷¨±È½ÏÆÕ±e£¬Òo´Ëµ¼ÖÂÔÚPHP4Öе±ÀaÔÚÒ»¸o´oÐ͵ÄÀa¼Ì³ÐÖнøÐÐÒƶ¯Ê±£¬´¦Ài½Ê½Óеa±¿×¾¡£µ±Ò»¸oÅÉÉuÀa±»Òƶ¯µ½Ò»¸o²»Í¬µÄ¸¸ÀaÖÐʱ£¬¸¸ÀaµÄ¹¹Ôi½¨Ãu±ØÈ»ÊDz»Í¬µÄ£¬ÕaÑuµÄ»°ÅÉÉuÀaÖеÄÓйص÷Óø¸Àa¹¹Ôi½¨µÄÓi¾aÐeÒª¸Äд¡£
PHP 5 introduces a standard way of declaring constructor methods by calling them by the name __construct().
PHP5ÒýÈeÁ˽¨Ãu__construct()À´¶¨Òa¹¹Ôi½*¨¡£
Example
<?php
class BaseClass {
function __construct() {
print "In BaseClass constructor\n";
}
}
class SubClass extends BaseClass {
function construct() {
parent::construct();
print "In SubClass constructor\n";
}
}
$obj = new BaseClass();
$obj = new SubClass();
?>
ΪÏoϼaeÈÝ£¬PHP5µ±ÔÚÀa²»ÄÜÕÒµ½construct()½¨Ê±£¬»aͨ¹ýÀϵĽ¨Ò²¾ÍÊÇÀaÃuÀ´²eÕÒ¹¹Ôi½¨¡£ÕaÒaζ×ÅΨһ¿ÉÄܲuÉu¼aeÈÝÐÔÎÊÌaµÄÊÇÔÚÒÔÇ°µÄ´uÂeÖÐÒѾʹÓÃÁËÒ»¸oÃuΪconstruct()µÄ½¨Ãu¡£
Îo¹¹½¨
¶¨ÒaÎo¹¹½¨ÊÇÊ®ÖÓÐÓõġ£Îo¹¹½¨¿ÉÒԼǼµ÷ÊÔÐÅÏ¢£¬¹Ø±ÕÊý¾Ý¿aÁ¬½Ó£¬»¹ÓÐ×oÆaËuµÄɨβ¹¤×÷¡£PHP4Öв¢ÎÞ´Ë»uÖÆ£¬¾¡¹ÜPHPÒÑÖ§³Ö×¢²aÔÚÇeÇo½aÊøʱÐeÒªÔËÐеĺ¯Êý¡£
PHP 5 introduces a destructor concept similar to that of other object-oriented languages, such as Java: When the last reference to an object is destroyed the object's destructor, which is a class method name %__destruct()% that recieves no parameters, is called before the object is freed from memory.
PHP5ÒýÈeÁËÓeÆaËuÃaeÏo¶ÔÏoÓiÑÔÈçJavaÓiÑÔÏaËƵÄÎo¹¹½¨£ºµ±×iºoÒ»¸o¸Ã¶ÔÏoµÄÒýÓñ»Ça³ýʱ£¬ÏµÍ³½«»aÔڸöÔÏo´ÓÄÚ´aeÖÐÊÍÅÇ°µ÷ÓÃÃuΪdestruct()µÄÎo¹¹½¨¡£
Àý£º
<?php
class MyDestructableClass {
function construct() {
print "In constructor\n";
$this->name = "MyDestructableClass";
}
function __destruct() {
print "Destroying " . $this->name . "\n";
}
}
$obj = new MyDestructableClass();
?>
ºÍ¹¹Ôi½¨ÏaËÆ£¬ÒýÇae½«²»µ÷Óø¸ÀaµÄÎo¹¹½¨£¬Îªµ÷Óøý¨£¬ÄaÐeÒªÔÚ×ÓÀaµÄÎo¹¹½¨ÖÐͨ¹ýparent::__destruct()Ói¾a½øÐе÷Óá£
³£Á¿
PHP 5 ÒýÈeÁËÀa³£Á¿¶¨Òa£º
<?php
class Foo {
const constant = "constant";
}
echo "Foo::constant = " . Foo::constant . "\n";
?>
PHP5ÔÊÐi³£Á¿ÖÐÓбi´iʽ£¬µ«ÔÚ±aÒeʱ³£Á¿Öеıi´iʽ½«±»¼ÆËa.£¬Òo´Ë³£Á¿²»ÄÜÔÚÔËÐÐÖиıaËuµÄÖµ¡£
<?php
class Bar {
const a = 1<<0;
const b = 1<<1;
const c = a | b;
}
?>
ÒÔÇ°´uÂeÖеÄÓû§×Ô¶¨ÒaÀa»o½¨ÖÐËa䶨Òa"const¡±¹Ø¼u×Ö£¬µ«ÎÞÐe±a¼¼´¿ÉÔËÐС£
ÀýÍa
PHP 4 had no exception handling. PHP 5 introduces a exception model similar to that of other programming languages.
PHP4ÖÐÎÞÀýÍa´¦Ài£¬PHP5ÒýÓÃÁËÓeÆaËuÓiÑÔÏaËƵÄÀýÍa´¦ÀiÄ£ÐÍ¡£
Àý£º
<?php
class MyExceptionFoo extends Exception {
function construct($exception) {
parent::construct($exception);
}
}
try {
throw new MyExceptionFoo("Hello");
} catch (MyException $exception) {
print $exception->getMessage();
}
?>
ÒÔÇ°´uÂeÖеÄÓû§×Ô¶¨ÒaÀa»o½¨ÖÐËa䶨Òa'catch', 'throw' ºÍ 'try'¹Ø¼u×Ö£¬µ«ÎÞÐe±a¼¼´¿ÉÔËÐС£
º¯Êýµ»Ø¶ÔÏoÖµ
In PHP 4 it wasn't possible to dereference objects returned by functions and make further method calls on those objects. With the advent of Zend Engine 2, the following is now possible:
ÔÚPHP4ÖУ¬º¯Êý²»¿ÉÄܵ»Ø¶ÔÏoµÄÖµ²¢¶Ôµ»ØµÄ¶ÔÏo½øÐн*¨µ÷Óã¬Í¨¹ýZENDÒýÇae2ÖУ¬ÕaÒ»ÇбaµÃ¿ÉÄÜ£º
<?php
class Circle {
function draw() {
print "Circle\n";
}
}
class Square {
function draw() {
print "Square\n";
}
}
function ShapeFactoryMethod($shape) {
switch ($shape) {
case "Circle":
return new Circle();
case "Square":
return new Square();
}
}
ShapeFactoryMethod("Circle")->draw();
ShapeFactoryMethod("Square")->draw();
?>
¾²Ì¬ÀaÖеľ²Ì¬³ÉÔ±±aÁ¿ÏÖÔڿɳoʼ»¯
Example
<?php
class foo {
static $my_static = 5;
}
print foo::$my_static;
?>
¾²Ì¬½¨
PHP5ÒýÈeÁ˹ؼu×Ö'static'À´¶¨ÒaÒ»¸o¾²Ì¬½¨£¬ÕaÑu¿ÉÒÔ´Ó¶ÔÏoÍa½øÐе÷Óá£
Àý£º
<?php
class Foo {
public static function aStaticMethod() {
// ...
}
}
Foo::aStaticMethod();
?>
ÐeÄa±aÁ¿$thisÔÚ¾²Ì¬½¨ÖÐÎÞЧ¡£
instanceof
PHP5ÒýÈeÁ˹ؼu×ÖinstanceofÀ´È¶¨Ò»¸o¶ÔÏoÊÇñÊÇijһ¸o¶ÔÏoµÄʵÀý£¬»oijһ¸o¶ÔÏoµÄÅÉÉu£¬»oʹÓÃÁËijһ¸o½Ó¿Ú¡£
Àý£º
<?php
class baseClass { }
$a = new baseClass;
if ($a instanceof basicClass) {
echo "Hello World";
}
?>
¾²Ì¬º¯Êý±aÁ¿
ËuÓеľ²Ì¬±aÁ¿ÏÖÔÚÔÚ±aÒeʱ½øÐд¦Ài£¬ÕaÔÊÐi¿ª¢Õßͨ¹ýÒýÓÃÀ´Ö¸¶¨¾²Ì¬±aÁ¿¡£Õa¸o±a»¯Ìa¸ßÁËЧÂʵ«Òaζ×Ų»¿ÉÄܶԾ²Ì¬±aÁ¿½øÐмa½ÓÒýÓá£
º¯ÊýÖÐͨ¹ý°´µØÖ´«ËÍ*½Ê½µÄ²ÎÊýÔÊÐi¶¨ÒaĬÈÏÖµ
Àý£º
<?php
function my_function(&$var = null) {
if ($var === null) {
die("$var needs to have a value");
}
}
?>
autoload()
ÔÚ³oʼ»¯Ò»¸o䶨ÒaµÄÀaʱ£¬ÒýÇae½«×Ô¶¯µ÷ÓÃautoload()À¹½ØÆ÷º¯Êý¡£¸ÃÀaÃu½«×÷Ϊautoload()À¹½ØÆ÷º¯ÊýΨһ²ÎÊý´«µÝ¸øËu¡£
Àý£º
<?php
function autoload($className) {
include_once $className . ".php";
}
$object = new ClassName;
?>
½¨ºÍÊoÐÔµ÷ÓõÄÖØÔØ
ͨÓà call(), get() ºÍ __set()½¨¿ÉÒÔ½øÐн¨ºÍÊoÐÔµ÷ÓõÄÖØÔØ¡£
Àý: get() ºÍ set()
<?php
class Setter {
public $n;
public $x = array("a" => 1, "b" => 2, "c" => 3);
function __get($nm) {
print "Getting [$nm]\n";
if (isset($this->x[$nm])) {
$r = $this->x[$nm];
print "Returning: $r\n";
return $r;
} else {
print "Nothing!\n";
}
}
function __set($nm, $val) {
print "Setting [$nm] to $val\n";
if (isset($this->x[$nm])) {
$this->x[$nm] = $val;
print "OK!\n";
} else {
print "Not OK!\n";
}
}
}
$foo = new Setter();
$foo->n = 1;
$foo->a = 100;
$foo->a++;
$foo->z++;
var_dump($foo);
?>
Àý: __call()
<?php
class Caller {
var $x = array(1, 2, 3);
function __call($m, $a) {
print "Method $m called:\n";
var_dump($a);
return $this->x;
}
}
$foo = new Caller();
$a = $foo->test(1, "2", 3.4, true);
var_dump($a);
?>