|
 |
栏目导栏 |
|
| |
|
|
|
|
 |
资料搜索 |
|
| |
|
|
|
|
 |
热门文章 |
|
| |
|
|
|
|
 |
最新文章 |
|
| |
|
|
|
| |
| |
|
|
|
|
类型指示 cHSLinux联盟 cHSLinux联盟 大家都知道,PHP是一种弱类型的语言。在使用变量前不需要定义,不需要声明变量的数据类型。这在编程中带来很多便利,但也带了一些隐患,特别当变量的类型变化时。在PHP5增加了类型指示,可以在执行过程中自动对类方法的参数类型进行判断。这类似于Java2中的RTTI,配合reflection可以让我们很好地控制对象。 cHSLinux联盟 cHSLinux联盟 <?php cHSLinux联盟 interface Foo { cHSLinux联盟 function a(Foo $foo); cHSLinux联盟 } cHSLinux联盟 cHSLinux联盟 interface Bar { cHSLinux联盟 function b(Bar $bar); cHSLinux联盟 } cHSLinux联盟 cHSLinux联盟 class FooBar implements Foo, Bar { cHSLinux联盟 function a(Foo $foo) { cHSLinux联盟 // ... cHSLinux联盟 } cHSLinux联盟 cHSLinux联盟 function b(Bar $bar) { cHSLinux联盟 // ... cHSLinux联盟 } cHSLinux联盟 } cHSLinux联盟 cHSLinux联盟 $a = new FooBar; cHSLinux联盟 $b = new FooBar; cHSLinux联盟 cHSLinux联盟 $a->a($b); cHSLinux联盟 $a->b($b); cHSLinux联盟 ?> cHSLinux联盟 cHSLinux联盟 在强类型语言中,所有变量的类型将在编译时进行检查,而在PHP中使用类型指示来对类型的检查则发生在运行时。如果类方法参数的类型不对,将会报出类似“Fatal error: Argument 1 must implement interface Bar…”这样的错误信息。 cHSLinux联盟 cHSLinux联盟 以下代码: cHSLinux联盟 cHSLinux联盟 <?php cHSLinux联盟 function foo(ClassName $object) { cHSLinux联盟 // ... cHSLinux联盟 } cHSLinux联盟 ?> cHSLinux联盟 cHSLinux联盟 相当于: cHSLinux联盟 cHSLinux联盟 <?php cHSLinux联盟 function foo($object) { cHSLinux联盟 if (!($object instanceof ClassName)) { cHSLinux联盟 die("Argument 1 must be an instance of ClassName"); cHSLinux联盟 } cHSLinux联盟 } cHSLinux联盟 ?> cHSLinux联盟 cHSLinux联盟 final关键字 cHSLinux联盟 cHSLinux联盟 PHP5中新增加了final关键字,它可以加在类或类方法前。标识为final的类方法,在子类中不能被覆写。标识为final的类,不能被继承,而且其中的方法都默认为final类型。 cHSLinux联盟 cHSLinux联盟 Final方法: cHSLinux联盟 cHSLinux联盟 <?php cHSLinux联盟 class Foo { cHSLinux联盟 final function bar() { cHSLinux联盟 // ... cHSLinux联盟 } cHSLinux联盟 } cHSLinux联盟 ?> cHSLinux联盟 cHSLinux联盟 Final类: cHSLinux联盟 cHSLinux联盟 <?php cHSLinux联盟 final class Foo { cHSLinux联盟 // class definition cHSLinux联盟 } cHSLinux联盟 cHSLinux联盟 // 下面这一行是错误的 cHSLinux联盟 // class Bork extends Foo {} cHSLinux联盟 ?> cHSLinux联盟 cHSLinux联盟 对象复制 cHSLinux联盟 cHSLinux联盟 前面在内存管理部份说过,PHP5中默认通过引用传递对象。像使用$object2=$object1这样的方法复制出的对象是相互关联的。如果我们确实需要复制出一个值与原来相同的对象而希望目标对象与源对象没有关联(像普通变量那样通过值来传递),那么就需要使用clone关键字。如果还希望在复制的同时变动源对象中的某些部份,可以在类中定一个__clone()函数,加入操作。 cHSLinux联盟 cHSLinux联盟 <?php cHSLinux联盟 //对象复制 cHSLinux联盟 class MyCloneable { cHSLinux联盟 static $id = 0; cHSLinux联盟 cHSLinux联盟 function MyCloneable() { cHSLinux联盟 $this->id = self::$id++; cHSLinux联盟 } cHSLinux联盟 cHSLinux联盟 cHSLinux联盟 /* cHSLinux联盟 function __clone() { cHSLinux联盟 $this->address = "New York"; cHSLinux联盟 $this->id = self::$id++; cHSLinux联盟 } cHSLinux联盟 */ cHSLinux联盟 } cHSLinux联盟 cHSLinux联盟 $obj = new MyCloneable(); cHSLinux联盟 cHSLinux联盟 $obj->name = "Hello"; cHSLinux联盟 $obj->address = "Tel-Aviv"; cHSLinux联盟 cHSLinux联盟 print $obj->id . "\n"; cHSLinux联盟 cHSLinux联盟 $obj_cloned = clone $obj; cHSLinux联盟 cHSLinux联盟 print $obj_cloned->id . "\n"; cHSLinux联盟 print $obj_cloned->name . "\n"; cHSLinux联盟 print $obj_cloned->address . "\n"; cHSLinux联盟 ?> cHSLinux联盟 cHSLinux联盟 以上代码复制出一个完全相同的对象。 cHSLinux联盟 cHSLinux联盟 然后请把function __clone()这一个函数的注释去掉,重新运行程序。则会复制出一个基本相同,但部份属性变动的对象。 cHSLinux联盟 cHSLinux联盟 类常量 cHSLinux联盟 cHSLinux联盟 PHP5中可以使用const关键字来定义类常量。 cHSLinux联盟 cHSLinux联盟 <?php cHSLinux联盟 class Foo { cHSLinux联盟 const constant = "constant"; cHSLinux联盟 } cHSLinux联盟 cHSLinux联盟 echo "Foo::constant = " . Foo::constant . "\n"; cHSLinux联盟 ?> cHSLinux联盟 cHSLinux联盟 __METHOD__常量 cHSLinux联盟 cHSLinux联盟 __METHOD__ 是PHP5中新增的“魔术”常量,表示类方法的名称。 cHSLinux联盟 cHSLinux联盟 魔术常量是一种PHP预定义常量,它的值可以是变化的,PHP中的其它已经存在的魔术常量有__LINE__、__FILE__、__FUNCTION__、__CLASS__等。 cHSLinux联盟 cHSLinux联盟 <?php cHSLinux联盟 class Foo { cHSLinux联盟 function show() { cHSLinux联盟 echo __METHOD__; cHSLinux联盟 } cHSLinux联盟 } cHSLinux联盟 cHSLinux联盟 class Bar extends Foo {} cHSLinux联盟 cHSLinux联盟 Foo::show(); // outputs Foo::show cHSLinux联盟 Bar::show(); // outputs Foo::show either since __METHOD__ is cHSLinux联盟 // compile-time evaluated token cHSLinux联盟 cHSLinux联盟 function test() { cHSLinux联盟 echo __METHOD__; cHSLinux联盟 } cHSLinux联盟 cHSLinux联盟 test(); // outputs test cHSLinux联盟 ?> cHSLinux联盟 cHSLinux联盟
Linux联盟收集整理 ,转贴请标明原始链接,如有任何疑问欢迎来本站Linux论坛讨论 |
|
|
|
|
|