1
I’m trying to give
require_oncein 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_onceinside the class builder, and it worked, now I attribute
$db_datafor an attribute and have everything within the class.
PS: When I read the error logs, they say that PHP Notice: Undefined variable.
– instalação planejamento
$db_data
came from where?– rray
Comes from require_once require_once('aps/config/database.php');
– instalação planejamento
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." }
.– Wesley Gonçalves
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.
– Wesley Gonçalves