gkent Posted November 1, 2011 Report Share Posted November 1, 2011 I apologize in advance if this question has a simple answer. I have had basic PHP programming experience for several years now; however, I have never ventured into object-oriented programming until now. I am trying to learn OOP and rebuild my website using various classes and objects. I can't seem to get beyond this initial problem of utilizing functions of classes from within other classes. Prior to writing on the forum, I referenced this thread (http://www.killersites.com/community/index.php?/topic/5770-new-to-oop-simple-question/) The goal is to create two files -- one (database.php) with all of the functions related to creation, manipulation, etc of databases, and the other (authentication.php) with functions related to user authentication and database confirmation. I would like to be able to call the database.php functions from within the authentication.php file. All of this would be done behind-the-scenes from my view file (login.php - or similar file). I am also using MDB2 from Pear for database connection framework utilizing mysqli. I tried to accomplish what was shared in the instructions; however, I am continuing to receive a fatal error (Fatal error: Call to a member function query() on a non-object). Here is the code from each of the pages. Login.php global $core; require_once "functions/core.php"; $core = new Core(); //Intialize Database Connection $core->db->do_connect(); //Deploy Authentication Class $core->auth->Authenticate($username, $password); Core.php require_once 'database.php'; require_once 'authentication.php'; class Core { public $db; public $auth; function __construct() { $this->db = new Database(); $this->auth = new Authentication(); } } Database.php class Database { //Create Variables private $dbuser; private $dbpwd; private $dbhost; private $dbname; public $con; //Construct MySQL_class public function __construct() { ; } public function do_connect() { //Setup Connection to MySQL Database $dbuser = "my-username-here"; $dbpwd = "my-password-here"; $dbhost = "my-host-here"; $dbname = "my-databasename-here"; //Include Required Files for PEAR MDB2 require_once 'MDB2.php'; //Intialize Database Connection $dsn = "mysqli://$dbuser:$dbpwd@$dbhost/$dbname"; $options = array("persistent" => true); $con =& MDB2::factory($dsn, $options); $con->setFetchMode(MDB2_FETCHMODE_ASSOC); } public function query($sql) { //Will eventually query database... no code written yet. } } Authentication.php class Authentication { //Construct Authentication Class function __construct() { global $core; } //Primary Function (to be called by login script) function Authenticate($username, $password) { //Check whether user exists in the database $this->UserExist($username); //Validate user's password with the database $this->PasswordMatch($username, $password); } function UserExist($username) { $sql = "SELECT * FROM tblUsers WHERE user_login = '$username' LIMIT 1"; //Code works to this point $core->db->query($sql); //This is what prompts the error. } function PasswordMatch($username, $password) { //No code written yet. Will be similar to UserExist } } Any help would be greatly appreciated! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.