2

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.

5
  • You need to pass it into the Database class. Commented Sep 28, 2015 at 19:19
  • What im trying is to set all variables in the App class and then access it in the database class through the extended "App" class. Commented Sep 28, 2015 at 19:20
  • That's not how extending works. Commented Sep 28, 2015 at 19:20
  • 1
    BTW, plural Library is Libraries Commented Sep 28, 2015 at 19:22
  • Ok, thats a detail. But how shoud i do that? is there a way to access the variables whitout passing it in every single class? i know static variables but thats not good practice they sad. Commented Sep 28, 2015 at 19:24

2 Answers 2

1

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
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Yeah, i know this, but you know i have very much classes and passing the variables through every single class is hard, i mean is there a simple way to do that whitout static variables?
see the edited answer. If you have a lot of classes and you have to pass some var throughout the whole structure — you have bad class design.
0

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"
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.