b0n Posted December 26, 2014 Share Posted December 26, 2014 This code works without problems: <?php namespace NamespaceA; class A extends \NamespaceB\B {} namespace NamespaceB; class B {} But why the following code cause Fatal error: Class 'NamespaceB\B' not found in ...file? <?php namespace NamespaceA; class A extends \NamespaceB\B {} namespace NamespaceB; class B extends \NamespaceC\C {} namespace NamespaceC; class C {} And this code also works without problems: <?php namespace NamespaceA; class A extends \NamespaceB\B {} namespace NamespaceC; class C {} namespace NamespaceB; class B extends \NamespaceC\C {} Without any namespace, also Fatal error: Class 'B' not found in ...file: <?php class A extends B {} class B extends C {} class C {} Works without problems: <?php class A extends B {} class B {} And yes everything is in the same PHP file.... Link to comment https://forums.phpfreaks.com/topic/293368-i-found-a-php-extends-bug/ Share on other sites More sharing options...
b0n Posted December 26, 2014 Author Share Posted December 26, 2014 Heck of a rabbit hole.... This works: <?php class B extends C {} class D extends C{} class E extends D{} class C{} ?> but this doesn't: <?php class B extends C {} class D extends C{} class C extends A{} class E extends D{} class A{} ?> is there anyway to contact the PHP developer team? Link to comment https://forums.phpfreaks.com/topic/293368-i-found-a-php-extends-bug/#findComment-1500726 Share on other sites More sharing options...
NotionCommotion Posted December 26, 2014 Share Posted December 26, 2014 Not a bug, just the way it is. If a class is being extended more than once, you need to put the classes in order so that when PHP interprets code, it is aware of the class. Link to comment https://forums.phpfreaks.com/topic/293368-i-found-a-php-extends-bug/#findComment-1500727 Share on other sites More sharing options...
b0n Posted December 26, 2014 Author Share Posted December 26, 2014 Not a bug, just the way it is. If a class is being extended more than once, you need to put the classes in order so that when PHP interprets code, it is aware of the class. <?php namespace NamespaceA; class A extends \NamespaceB\B {} namespace NamespaceC; class C {} namespace NamespaceB; class B extends \NamespaceC\C {} why this work? Link to comment https://forums.phpfreaks.com/topic/293368-i-found-a-php-extends-bug/#findComment-1500728 Share on other sites More sharing options...
NotionCommotion Posted December 26, 2014 Share Posted December 26, 2014 Same reason this works: class A extends B{} class C{} class B extends C{} and this doesn't: class A extends B{} class B extends C{} class C{} I don't know the specifics, but per http://php.net/manual/en/keyword.extends.php: 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. Granted, one would expect the first example not to work. Better off always putting them in order. Link to comment https://forums.phpfreaks.com/topic/293368-i-found-a-php-extends-bug/#findComment-1500730 Share on other sites More sharing options...
hansford Posted December 26, 2014 Share Posted December 26, 2014 namespace NamespaceA { class A extends \NamespaceB\B { function __construct() { echo "class A<br />"; parent::__construct(); } } } namespace NamespaceC { class C { function __construct() { echo "class C<br />"; } } } namespace NamespaceB { class B extends \NamespaceC\C { function __construct() { echo "class B<br />"; parent::__construct(); } } } I will grant you that it is odd behavior. However, namespaces were designed to be located each in their own file. That's my story and I'm sticking to it! Link to comment https://forums.phpfreaks.com/topic/293368-i-found-a-php-extends-bug/#findComment-1500738 Share on other sites More sharing options...
b0n Posted December 26, 2014 Author Share Posted December 26, 2014 So I guess php just kinda does what it wants? Not really any set in stone rules? Link to comment https://forums.phpfreaks.com/topic/293368-i-found-a-php-extends-bug/#findComment-1500749 Share on other sites More sharing options...
kicken Posted December 26, 2014 Share Posted December 26, 2014 So I guess php just kinda does what it wants? Not really any set in stone rules? No, the rule is set. Your child classes must come after your base classes. There is probably some internal implementation detail that currently allows for the one case to work (my guess would be something regarding auto-loading) however this is something that could change at any time and should never be relied upon. Link to comment https://forums.phpfreaks.com/topic/293368-i-found-a-php-extends-bug/#findComment-1500756 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.