Objects in Classes
Moderator: General Moderators
-
chris12295
- Forum Contributor
- Posts: 113
- Joined: Sun Jun 09, 2002 10:28 pm
- Location: USA
- Contact:
Objects in Classes
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?
I want a db reader class to create a new mysqli but
I get an error. Is this possible?
-
blackbeard
- Forum Contributor
- Posts: 123
- Joined: Thu Aug 03, 2006 6:20 pm
-
chris12295
- Forum Contributor
- Posts: 113
- Joined: Sun Jun 09, 2002 10:28 pm
- Location: USA
- Contact:
Code: Select all
<?php
class user
{
private $host = "****";
private $username = "****";
private $password = "****";
private $database = "****";
private $db = new mysqli($host, $username, $password, $database);
function __construct() {
}
}
?>-
chris12295
- Forum Contributor
- Posts: 113
- Joined: Sun Jun 09, 2002 10:28 pm
- Location: USA
- Contact:
also, is it possible to do something like
if i have already created a mysqli object elsewhere?
Code: Select all
$db = $GLOBALS[db];- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Read this manual page, just above figure 19.3...
Declare the var, then in the contructor, instantiate the object.
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() {
}
}
?>
Last edited by RobertGonzalez on Tue Feb 13, 2007 5:40 pm, edited 1 time in total.
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);
}
}-
chris12295
- Forum Contributor
- Posts: 113
- Joined: Sun Jun 09, 2002 10:28 pm
- Location: USA
- Contact:
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
-
chris12295
- Forum Contributor
- Posts: 113
- Joined: Sun Jun 09, 2002 10:28 pm
- Location: USA
- Contact:
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
-
chris12295
- Forum Contributor
- Posts: 113
- Joined: Sun Jun 09, 2002 10:28 pm
- Location: USA
- Contact:
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA