开发者

is it a php bug? (about extends)

开发者 https://www.devze.com 2023-03-13 06:47 出处:网络
class A extends B {} class B extends C{} class C {} result PHP Fatal error: class \'B\' not found ... if the order is like this
class A extends B {}
class B extends C{}
class C {}

result

PHP Fatal error: class 'B' not found ...

if the order is like this

class A ex开发者_C百科tends B {}
class C {}
class B extends C{}

everything is ok.


PS: if I remove class C {}

class A extends B {}
class B extends C{}

php tells me class 'B' is not found, why?

php version 5.3.4


The PHP manual clearly mentions:

Classes must be defined before they are used! If you want the class Named_Cart to extend the class Cart, you will have to define the class Cart first. If you want to create another class called Yellow_named_cart based on the class Named_Cart you have to define Named_Cart first. To make it short: the order in which the classes are defined is important.


clearly a parser bug

this works

class A extends B {}
class B {}

this doesn't

class C extends D {}
class D extends E {}
class E {}

consider reporting on bugs.php.net


Class order matters in PHP definitions.

Does the order of class definition matter in PHP?

This is why you don't have visibility of the class defined after the one you are defining (in this case class A cant see class B because it is defined after).


Because php is interpreted rather than compiled the order of declaration must be valid. In this example class B doesn't exist for A to extend.


Obivously class B is not defined in the moment you trying to extend it, because it occurs after extends B. Its not a bug, its the way the world works: You can only use, what exists ;)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号