Jump to content

I found a PHP 'extends' bug....


b0n

Recommended Posts

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

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?

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?

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.
 

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!

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.