类型指示
大家都知道,php是一种弱类型的语言。在使用变量前不需要定义,不需要声明变量的数据类型。这在编程中带来很多便利,但也带了一些隐患,特别当变量的类型变化时。在php5增加了类型指示,可以在执行过程中自动对类方法的参数类型进行判断。这类似于java2中的rtti,配合reflection可以让我们很好地控制对象。
<?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);
?>
在强类型语言中,所有变量的类型将在编译时进行检查,而在php中使用类型指示来对类型的检查则发生在运行时。如果类方法参数的类型不对,将会报出类似“fatal error: argument 1 must implement interface bar…”这样的错误信息。
以下代码:
<?php
function foo(classname $object) {
// ...
}
?>
相当于:
<?php
function foo($object) {
if (!($object instanceof classname)) {
die("argument 1 must be an instance of classname");
}
}
?>
final关键字
php5中新增加了final关键字,它可以加在类或类方法前。标识为final的类方法,在子类中不能被覆写。标识为final的类,不能被继承,而且其中的方法都默认为final类型。
final方法:
<?php
class foo {
final function bar() {
// ...
}
}
?>
final类:
<?php
final class foo {
// class definition
}
// 下面这一行是错误的
// class bork extends foo {}
?>
对象复制
前面在内存管理部份说过,php5中默认通过引用传递对象。像使用$object2=$object1这样的方法复制出的对象是相互关联的。如果我们确实需要复制出一个值与原来相同的对象而希望目标对象与源对象没有关联(像普通变量那样通过值来传递),那么就需要使用clone关键字。如果还希望在复制的同时变动源对象中的某些部份,可以在类中定一个__clone()函数,加入操作。
<?php
//对象复制
class mycloneable {
static $id = 0;
function mycloneable() {
$this->id = self::$id++;
}
/*
function __clone() {
$this->address = "new york";
$this->id = self::$id++;
}
*/
}
$obj = new mycloneable();
$obj->name = "hello";
$obj->address = "tel-aviv";
print $obj->id . "/n";
$obj_cloned = clone $obj;
print $obj_cloned->id . "/n";
print $obj_cloned->name . "/n";
print $obj_cloned->address . "/n";
?>
以上代码复制出一个完全相同的对象。
然后请把function __clone()这一个函数的注释去掉,重新运行程序。则会复制出一个基本相同,但部份属性变动的对象。
类常量
php5中可以使用const关键字来定义类常量。
<?php
class foo {
const constant = "constant";
}
echo "foo::constant = " . foo::constant . "/n";
?>
__method__常量
__method__ 是php5中新增的“魔术”常量,表示类方法的名称。
魔术常量是一种php预定义常量,它的值可以是变化的,php中的其它已经存在的魔术常量有__line__、__file__、__function__、__class__等。
<?php
class foo {
function show() {
echo __method__;
}
}
class bar extends foo {}
foo::show(); // outputs foo::show
bar::show(); // outputs foo::show either since __method__ is
// compile-time evaluated token
function test() {
echo __method__;
}
test(); // outputs test
?>