Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it a php bug? (about extends)

Tags:

php

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 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

like image 996
limboy Avatar asked Jul 12 '26 00:07

limboy


1 Answers

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.

like image 143
codaddict Avatar answered Jul 14 '26 14:07

codaddict



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!