Is this a php error? (about expansion)

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 C {}
class B extends C{}

everything is fine.


PS: if I delete the class C {}

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

php says class “B” not found, why?

php version 5.3.4

+3
source share
5 answers

clearly a parser error

it works

class A extends B {}
class B {}

is not

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

view bugs.php.net reporting

+2
source

The PHP manual clearly mentions:

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

+5

PHP.

PHP?

This is why you do not have the visibility of the class defined after the one you define (in this case, class Asee cant class Bbecause it is defined after).

0
source

Since php is interpreted rather than compiled, the declaration order must be valid. In this example, class B does not exist for extension A.

0
source

Abundantly class Bnot determined at the moment when you are trying to expand it, because it happens after extends B. This is not a mistake, its way of working in the world: you can use only what exists;)

0
source

All Articles