}
}
$obj = new MyDestructableClass();
?>
和构造方法相似,引擎将不调用父类的析构方法,为调用该方法,你需要在子类的析构方法中通过parent::__destruct()语句进行调用。
常量
PHP 5 引入了类常量定义:
<?php
class Foo {
const constant = "constant";
}
echo "Foo::constant = " . Foo::constant . "n";
?>
PHP5允许常量中有表达式,但在编译时常量中的表达式将被计算.,因此常量不能在运行中改变它的值。
<?php
class Bar {
const a = 1<<0;
const b = 1<<1;
const c = a | b;
}
?>
以前代码中的用户自定义类或方法中虽未定义"const”关键字,但无需编辑即可运行。
例外
PHP 4 had no exception handling. PHP 5 introduces a exception model similar to that of other programming languages.
PHP4中无例外处理,PHP5引用了与其它语言相似的例外处理模型。
例:
<?php
class MyExceptionFoo extends Exception {
function __construct($exception) {
parent::__construct($exception);
}
}
try {
throw new MyExceptionFoo("Hello");
} catch (MyException $exception) {
print $exception->getMessage();
}
?>
以前代码中的用户自定义类或方法中虽未定义'catch', 'throw' 和 'try'关键字,但无需编辑即可运行。
函数返回对象值
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中,函数不可能返回对象的值并对返回的对象进行方法调用,通过ZEND引擎2中,这一切变得可能:
<?php
class Circle {
function draw() {
print "Circlen";
}
}
class Square {
function draw() {
print "Squaren";
}
}
function ShapeFactoryMethod($shape) {
switch ($shape) {
case "Circle":
return new Circle();
case "Square":
return new Square();
}
}
ShapeFactoryMethod("Circle")->draw();
ShapeFactoryMethod("Square")->draw();
?>
静态类中的静态成员变量现在可初始化
Example
<?php
class foo {
static $my_static = 5;







