0

I have some troubles with inheritance. I have something like that

file A.php

namespace Main;
class A{
    public function __construct(){ echo 'A->__construct'; }
}

and file B.php

namespace Main;
class B extends \Main\A{
    /* if I write this construct and remove extends from this class - it works - but I need to inherit it */
    public function __construct(){ $a = new \Main\A(); }

    public function something(){ echo 'B->something';}
}

Are there some cases when classes cannot be inherited or inherit another class?

2 Answers2

1

I had a similar problem that was driving me crazy.

As a test, I even copied the parent class, renamed it (i.e. the code of the abstract class is EXACTLY the same apart from the file name and class names) and inherit from that the problem was 'fixed'. Weird!.

In my case I found that another class had a require_once for the class that was causing the error (i.e. 'B' in the original poster's example) and removing (e.g.) 'require_once B' fixed it. Somehow I must have introduced something circular this was causing PHP to not load the class - I have NO idea why, if anyone has ideas, I'd love to increase my knowledge.

Anyway... I have just moved over to using spl_autoload_register (definitely the way to go). Now using spl_autoload_register in conjunction with removing require_once whenever I find one seems to have solved the problem.

I know this reply is too late for the original poster, but I hope this helps someone else that is pulling their hair out (like I was) with a similar issue.

Sherif
  • 11
  • 2
0

This works.

namespace Main;
class A
{
    public function __construct(){ echo 'A->__construct'; }
}

class B extends  A
{

    public function something(){ echo 'B->something';}
}

$test = new B();

output :

A->__construct

so does this :

namespace Main;
class A
{
    public function __construct(){ echo 'A->__construct'; }
}

class B extends  \Main\A
{

    public function something(){ echo 'B->something';}
}

$test = new B();

A->__construct

Are you missing

require_once "A.php"; 

in the B.php file?

You can test putting both class definitions in the same file to see if your issue is related to the inclusion of A.php. Further do you get an error message?

Gavin
  • 2,153
  • 2
  • 25
  • 35
  • This is piece of code (which works) is just part of my bigger project (where it doesn't work) - everything ir required I'm using autoloader to do it... I'd like to know answer to this question "Are there some cases when classes cannot be inherited or inherit another class?" – StellaireMan Jul 31 '13 at 20:50
  • If the inheritance failed you should expect an error message. More than one definition of A is a possibility, meaning not inheriting from the one that you expect? Try both definitions in the same file just for curiosity. Also check the output of get_parent_class(new B()); – Gavin Jul 31 '13 at 21:01
  • I'm trying it but every time it fails - Fatal error: Class 'Singular\Object' not found in D:\xampp\htdocs\singular\libs\Singular\Bootstrap.php on line 10 (and for sure - I have all files requred) – StellaireMan Jul 31 '13 at 21:10
  • Your error message indicates that the class definition for Singular\Object was not loaded at the time the class was instantiated. Is it possible that this class is getting instantiated before the autoloader is initialised? Or that the autoloader is failing? this looks more like a file inclusion problem or a namespace problem than inheritance. Or .. are you missing a namespace in front of the class when instantiating, such as $test = new main\A(); rather than $test = new A(); – Gavin Jul 31 '13 at 21:26
  • 1
    as I wrote.. creating instance works (so file is loaded) but inheriting doesn't.. I ran autoloader first and after that this classes – StellaireMan Jul 31 '13 at 21:44