Let's pretend I have a database.php file which contains a persistent access to the database.
<?php
$database = new PDO('mysql:host=xxx', "xxx", "xxx", array(
PDO::ATTR_PERSISTENT => true
));
?>
Every time I want to query the database within a controller, what should I do?
1/ Use the global keyword to get my global $database variable
<?php
include '../app/config/Database.php';
function getLastOfTheWeek()
{
global $database;
$database->query('SELECT * FROM `xxx`');
...
}
?>
2/ Include database.php within the function
<?php
function getLastOfTheWeek()
{
include '../app/config/Database.php';
$database->query('SELECT * FROM `xxx`');
...
}
?>
3/ Give this man some doc, he needs it
Or both are evil anyway, and I should use another method.