php - Autoloading a database class within another class? -
i have 2 classes, database , user. in database class have function connect database. wanting able have connection database within user class. doing in user class:
class user { function __construct() { require_once 'database.class.php'; $dbh = new database(); $dbh->connect(); } function register_user() { $dbh->prepare('insert users values (:username, :password, :forename, :surname)'); $dbh->execute(array(':username' => 'administrator', ':password' => '5f4dcc3b5aa765d61d8327deb882cf99', ':forename' => 'richie', ':surname' => 'jenkins')); } } i following error:
php fatal error: call member function prepare() on non-object
you should read "scope." $dbh declared locally in __construct().
rectifying easy. add
class user { private $dbh; and wherever have $dbh change $this->dbh. may read $this , member variables well.
Comments
Post a Comment