require_once does not work

Asked

Viewed 1,254 times

1

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 briefly, sorry for the inconvenience.

Anyway, I did the

require_once
inside the class builder, and it worked, now I attribute
$db_data
for an attribute and have everything within the class.

  • PS: When I read the error logs, they say that PHP Notice: Undefined variable.

  • $db_data came from where?

  • Comes from require_once require_once('aps/config/database.php');

  • Comment for your reply: If you want, you can handle the error by checking the function return require_once. $retorno = require_once('aps/config/database.php'); if ($retorno === false) { echo "Error." }.

  • You can answer your own question and accept it as an answer. This makes it clearer to those who see your question later. And, it still makes it possible for you to earn more points.

2 answers

3

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);
  • Thanks rray, it’s an option, however I could not be passing as parameter, I wanted to do something more like the structure of Codeigniter, you know? But it’s an option, thank you!

  • @installation Codeigniter planning a include within a method looks at the code

  • Yes yes! I realize that now! Thank you!

0

Mano is a fact that has error, only has to require_once produces an error treatment that terminates the script, uses include_once in place that it will display you the error and you see what is wrong and fixes, ex:

include_once "aps/config/database.php";

And more, declare out of class, ex:

include_once "aps/config/database.php";
class CoreDatabase {}
  • The require_once also produces an error message. The difference is that it produces an error fatal that will stop the script (E_COMPILE_ERROR) while the include_once or generate only a warning (E_WARNING).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.