I'm trying to extend a class with the variables in the constructor. Here a little example.
I have my index.php with the following code in it.
<?php
namespace System;
require_once 'App/Config.php';
spl_autoload_register(function($class) use ($config) {
require_once $config['app']['root'] . '/' . $class . '.php';
});
$app = new App($config);
$app->Start();
?>
All is working fine. Now I in the constructor of the class App passed the config file.
<?php
namespace System;
use System\Librarys\Database;
class App
{
protected $config;
protected $connection;
public function __construct($config)
{
$this->config = $config;
$this->connection = $this->getConnection();
}
public function getConnection()
{
$this->connection = new Database;
$this->connection = $this->connection->Connect();
return $this->connection;
}
public function Start()
{
echo 'test';
}
public function __destruct()
{
$this->config = null;
$this->connection = null;
}
}
?>
Ok, all good! But now, I want to make the database connection. I extended the "App" class in the database class. Like below:
<?php
namespace System\Librarys;
use System\App;
class Database extends App
{
public function __construct()
{
parent::__construct(??? HOW DO I GET THE VARIABLE FROM THE "APP" CLASS ???);
var_dump($this->config);
}
}
?>
And now, if I do a var_dump() on $this->config it returns null. That's clear because I didn't pass the $config var in the parent constructor. But how do I do that? I want to set all variables in the App class so I can extend it, and don't need to pass the variables to the other classes.
Databaseclass.