Jump to content

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


b0n
Go to solution Solved by kicken,

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

Link to comment
Share on other sites

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?

Edited by b0n
Link to comment
Share on other sites

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.
 

Edited by NotionCommotion
Link to comment
Share on other sites

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
Share on other sites

  • Solution

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.

Edited by kicken
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.