passing an database object to a class

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
php3ch0
Forum Contributor
Posts: 212
Joined: Sun Nov 13, 2005 7:35 am
Location: Folkestone, Kent, UK

passing an database object to a class

Post by php3ch0 »

OK so I am starting in the world of OOP and I have the need to query the database from another class from the database one. This is the code I have so far

page.inc.php (where I include files etc and create the $db object)

Code: Select all

require_once("db.class.php"); //the db class

$db = new DB($g_db, "localhost", $g_dbuser, $g_dbpass);

require_once("user.inc.php");

$user = new user($_SESSION['uid']);

The user.inc.php class (this is where the error is)

Code: Select all

class user {
	

	var $uid; // Database object
	protected $dbo; 
		
	public function __construct(&$db) { 
		$this ->dbo = &$db; 
		}

	function signin($uid) {
			$userdata = $this->dbo->query_UniqueObject("SELECT * FROM users WHERE id = '$uid'");
			$_SESSION['uid'] = $userdata->id;
			$_SESSION['al'] = $userdata->access_level;
			session_write_close();
			}
}
This is the error I am getting: Fatal error: Call to a member function query_UniqueObject() on a non-object in /home/combatli/public_html/includes/user.inc.php on line 14

I know I can declare $db as a global but I want to avoid it (even tried it and cannot get it to work) by adding it after here function signin($uid) { global $db; Any help would be appreciated.
x_mutatis_mutandis_x
Forum Contributor
Posts: 160
Joined: Tue Apr 17, 2012 12:57 pm

Re: passing an database object to a class

Post by x_mutatis_mutandis_x »

$this->dbo is not being set because you are passing a string ($_SESSION['uid']) instead of $db from page.inc.php. Your class user accepts a DB object in the constructor

Code: Select all

public function __construct(&$db) { 
                $this ->dbo = &$db; 
                }
 
So, change page.inc.php:

Code: Select all


require_once("db.class.php"); //the db class
 
$db = new DB($g_db, "localhost", $g_dbuser, $g_dbpass);
 
require_once("user.inc.php");

//$user = new user($_SESSION['uid']);
$user = new user($db);
 
User avatar
php3ch0
Forum Contributor
Posts: 212
Joined: Sun Nov 13, 2005 7:35 am
Location: Folkestone, Kent, UK

Re: passing an database object to a class

Post by php3ch0 »

Hello I have changed this but still getting the same error. Any other suggestions?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: passing an database object to a class

Post by pickle »

You've got a space in here: $this ->dbo, maybe that's causing it. Other than that, check in the constructor of User to see if $this->dbo is being set properly.

Also, if you're using PHP5, you don't need to to = &$db, as objects are assigned by reference by default.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply