require_once does not work

0

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 not get 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 Galera, I solved briefly, sorry for the inconvenience.

Anyway, I did the

require_once
within the constructor of the class, and it worked, now I assign
$db_data
to an attribute and I have everything inside the class.     
asked by anonymous 14.11.2017 / 18:29

2 answers

2

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

Change:

public function __construct() {

To:

public function __construct($db_data) {

When instantiating this class remember to pass the variable.

$dbCore = new CoreDatabase($db_data);
    
14.11.2017 / 18:35
1

Hand is fact that has error, just have that require_once produces an error handling that terminates the script, uses include_once instead that it will display the error and you see what is wrong and corrects, eg:

include_once "aps/config/database.php";

And there's more, declare outside the class, eg:

include_once "aps/config/database.php";
class CoreDatabase {}
    
14.11.2017 / 18:41