I have an PDO object in an included file and when i use it in main page it works great. And when i pass it into an object to use it inside of them it simply dont work.
I've tried directly and with reference (function xxxx(&dbd){ this->$db = &dbd }), simply dont work, but if i pass another type of value (as a string) that works perfect. If i send a $db = "olaola" it works but if it is an PDO it fails. I'm a newbie in php and in english, so be patient please :P
included file:
$username = "root"; $password = "*****"; $host = "localhost"; $dbname = "dbname"; $db = NULL; $options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'); try { $db = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options); } catch(PDOException $ex) { die("Failed to connect to the database: " . $ex->getMessage()); } $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); in main:
$pagMenu = new pages($db); in pages class:
class pages { private $db; function __construct($db) { $this->$db = $db; } }