实例化后的声明仅在类未实现接口时才有效


new A;
class A {}
new C;
class C extends A {}
new B;
class B implements iB {}
interface iB {} // we can even put interface definition after the class
                // that implements it

我希望这段代码工作没有错误,而我得到

PHP: Class 'B' not found in…

有人能解释一下为什么会这样吗?这是有记录的行为吗?

PS: php 5.3.2

PPS:同样的行为在ideone: http://ideone.com/q4cT6A

这是PHP中预期的。很抱歉给你这样的答案,我也曾预料到这一点。;)

引用自PHP文档:

除非使用自动加载,否则类必须在使用之前定义。如果一个类继承了另一个类,那么必须在子类结构之前声明父类。此规则适用于继承其他类和接口的类。

最新更新