Objects in Classes

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
chris12295
Forum Contributor
Posts: 113
Joined: Sun Jun 09, 2002 10:28 pm
Location: USA
Contact:

Objects in Classes

Post by chris12295 »

Is it possible to instantiate an object inside a class?
I want a db reader class to create a new mysqli but
I get an error. Is this possible?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Not only is it possible, it's a pretty normal activity.

Post your code. :)
blackbeard
Forum Contributor
Posts: 123
Joined: Thu Aug 03, 2006 6:20 pm

Post by blackbeard »

Yes, you can. What exactly is the error?
chris12295
Forum Contributor
Posts: 113
Joined: Sun Jun 09, 2002 10:28 pm
Location: USA
Contact:

Post by chris12295 »

Code: Select all

<?php

class user 
{
 
	private $host = "****";
	private $username = "****";
	private $password = "****";
	private $database = "****";
	private $db = new mysqli($host, $username, $password, $database);
	
	function __construct() {
	}
	
}

?>
and the error is: Parse error: syntax error, unexpected T_NEW in users/inc/classes/class.user.php on line 10
chris12295
Forum Contributor
Posts: 113
Joined: Sun Jun 09, 2002 10:28 pm
Location: USA
Contact:

Post by chris12295 »

also, is it possible to do something like

Code: Select all

$db = $GLOBALS[db];
if i have already created a mysqli object elsewhere?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Read this manual page, just above figure 19.3...

Code: Select all

<?php

class user 
{
 
	private $host = "****";
	private $username = "****";
	private $password = "****";
	private $database = "****";

	// I AM ALMOST CERTAIN THIS IS YOUR PROBLEM...
	private $db = new mysqli($host, $username, $password, $database);
	
	function __construct() {
	}
	
}

?>
Declare the var, then in the contructor, instantiate the object.
Last edited by RobertGonzalez on Tue Feb 13, 2007 5:40 pm, edited 1 time in total.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Code: Select all

class user
{
 
        private $host = "****";
        private $username = "****";
        private $password = "****";
        private $database = "****";
        private $db;
       
        function __construct() {
                $this->db = new mysqli($this->host, $this->username, $this->password, $this->database);
        }
       
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

If you're embracing OOP, don't use globals. :)
chris12295
Forum Contributor
Posts: 113
Joined: Sun Jun 09, 2002 10:28 pm
Location: USA
Contact:

Post by chris12295 »

the above worked, is there anyway to be able to use $db instead of $this->db?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Not without globalizing. Why would you want to? Other than because you don't want to do a find and replace throughout the class and change all $db into $this->db...:wink:
chris12295
Forum Contributor
Posts: 113
Joined: Sun Jun 09, 2002 10:28 pm
Location: USA
Contact:

Post by chris12295 »

well just to make typing easier, i dont like using $this->db->query() for example but I do like using mysqli as an object
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Trust me, using $this->db->query is cake compared to $this->settings->get('object.mysql')->result->num_rows....
chris12295
Forum Contributor
Posts: 113
Joined: Sun Jun 09, 2002 10:28 pm
Location: USA
Contact:

Post by chris12295 »

true. thanks for all the help.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You got it. Glad we could help.
Post Reply