I have read a lot of threads, and PLEASE, just remain to what the question is, probably sounds like a duplicated entry, but sincerely, I've not understand them correctly, so I am looking forward for a reasonable explanation.
I am recreating a Framework (not to be implemented, just to learn how Frameworks work). Structure looks like this:
App
App/Core
App/Core/Controllers
App/Core/Controllers/Home
App/Core/Models
App/Core/Views
App/Libraries
App/Libraries/vendor
App/Libraries/composer.json
App/init.php
index.php
.htaccess
this is my composer.json file
{
"autoload": {
"psr-4": {
"App\\": "App"
}
}
}
this is my index.php file
const CONTROLLERS = 'App/Core/Controllers/';
require_once 'App/init.php';
$time = microtime(true);
new App\Init();
this is my init.php class
namespace App;
require_once __DIR__ . '/Libraries/vendor/autoload.php';
use App\Controller;
class Init
{
private $resource;
private $object;
private $file;
const HOME = 'home';
function __construct()
{
$this->resource = empty($_GET) ? array(self::HOME) : self::Uri($_GET['resource']);
$class = self::Object($this->resource);
new App\Controller\$class;
}
private function Uri($_resource)
{
if($_resource != ucfirst(self::HOME))
{
$this->resource = explode('/', filter_var(rtrim($_resource, '/'), FILTER_SANITIZE_URL));
}
return $this->resource;
}
private function Object($_resource)
{
$this->object = ucfirst($_resource[0]);
$this->file = CONTROLLERS . $this->object . '.php';
if(file_exists($this->file))
{
require $this->file;
return $this->object;
}
else
{
echo 'Nope';
}
}
}
This is my Home.php class
namespace App\Controller;
class Home
{
public function __construct()
{
echo '<br>I am at home';
}
}
The error I get is:
Parse error: syntax error, unexpected '$class' (T_VARIABLE), expecting identifier (T_STRING)
Please please help me out here, driving me crazy trying to understand why if I use new App\Controller\Home; it works, but if I use $class it crashes. I even tried new App\Controller{$class} with no luck.
Anyway hoping to get an answer a clear answer, because probably a lot of people would go through the same issue
Thank you all in advance