require once does not work

I'm trying to give

require_once
in a PHP file that has a class.

The file I'm trying to include in PHP with the class has only one array with configuration data, but I can't access it properly.

class CoreDatabase {

    public $database;

    public function __construct() {
        require_once('aps/config/database.php');
        $this->database = new PDO($db_data['default']['driver'] . ':host=' . $db_data['default']['host'] . ';dbname=' . $db_data['default']['name'], $db_data['default']['user'], $db_data['default']['password']);

        $statement = $this->database->prepare('select * from tablex');

        $statement->execute();
        echo var_dump($statement->fetch(PDO::FETCH_ASSOC));
        echo var_dump($this->database);
        echo var_dump($data);
        echo var_dump($statement);
    }

Edited Guys, I solved it briefly, sorry for the trouble.

Anyway, I did the

require_once
inside the class constructor, and it worked out, now I assign
$db_data
to an attribute and have everything inside the class.
Author: instalação planejamento, 2017-11-14

2 answers

If $db_data is the configuration variable that comes from aps/config/database.php it must be passed as an argument in the constructor so the class can access it.

Change:

public function __construct() {

For:

public function __construct($db_data) {

When instantiating this class remember to pass the variable.

$dbCore = new CoreDatabase($db_data);
 3
Author: rray, 2017-11-14 17:35:36

Mano is fact that has error, just have to require_once produces an error treatment that terminates the script, uses include_once in the place that it will show you the error and you see what is wrong and corrects, ex:

include_once "aps/config/database.php";

And has more, declare outside the class, ex:

include_once "aps/config/database.php";
class CoreDatabase {}
 0
Author: Brenddon Anjos, 2017-11-14 17:41:09