Sir Akaya
Sir Akaya

Reputation: 43

Variables of extended class (PHP)

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.

Upvotes: 2

Views: 79

Answers (2)

Zsw
Zsw

Reputation: 4097

When you call __construct() on Database, you can't get $this->config from App because it's not set yet.

You have to set the variable in the constructor before you can use it.

class Database extends App
{
    public function __construct()
    {
        parent::__construct("localhost");
        var_dump($this->config); // $this->config is now "localhost"
    }
}

Upvotes: 0

Timofey
Timofey

Reputation: 829

It's not clear to me why you just doesn't use the same constructor on Database class. The code would be like this:

public function __construct($config)
{
   parent::__construct($config);
}

And then in App class

$this->connection = new Database($this->config);

By the way, if you're not going to add any more code to the Database constructor, you don't actually need it.

P.S. I see in your code bad class design. You're probably using the App class for global configuration and the database connection is a part of it. So you need to create a single class that would handle all database operations. Then you just use it in your App's instance. For instance:

class DB {
    function connect() { /* Some code */ }
    // More functions
}

class App {
    protected $db;
    // contructorts etc
    function run() {
        $this->db = new DB(/* some config */);
        // use it
    }
}

Upvotes: 1

Related Questions