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