类型指示
大家都知道,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是一种弱类型的语言。在使用变量前不需要定义,不需要声明变量的数据类型。这在编程中带来很多便利,但也带了一些隐患,特别当变量的类型变化时。在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);
?>
| 对此文章发表了评论 |
