I have three different files for each part of code. (i use class autoloader). First file (/classes/Test/Foo.php):
<?php
namespace Test;
class Foo
{
public $id;
function __construct(int $id)
{
$this->id = $id;
}
public function get() : string
{
return "FOO:" . $this->id;
}
}
Second file (/classes/Test/Bar.php):
<?php
namespace Test;
class Bar extends Foo
{
public function get() : string
{
return "BAR:" . $this->id;
}
}
Third file (/index.php):
<?php
namespace Test;
include "autoloader.php";
$bar = new Bar(1);
echo $bar->get();
When i execute third file i didn't get any output at all, not even error ...but if i put all code in one file it works.
<?php
namespace Test;
class Foo
{
public $id;
function __construct(int $id)
{
$this->id = $id;
}
public function get() : string
{
return "FOO:" . $this->id;
}
}
class Bar extends Foo
{
public function get() : string
{
return "BAR:" . $this->id;
}
}
$bar = new Bar(1);
echo $bar->get();
output: BAR:1
What could be the problem?
Autoloader code:
<?php
spl_autoload_register(function($class) {
require_once $_SERVER['DOCUMENT_ROOT'] . "/classes/" . str_replace("\\", "/", $class) . ".php";
});