Passing SQL Object into new class
Moderator: General Moderators
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Passing SQL Object into new class
You showed to different Achievements classes that have different constructor arguments. According to the error message in the posts higher up you are not passing an object to the constructor that has a query() method. In the previous post you created and assign the $db property inside the constructor. Are you sure you are not trying the first class but passing $host as the first parameter somewhere.
(#10850)
Re: Passing SQL Object into new class
Positive i've showed you the final code.
and the changes i made.
and the changes i made.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Passing SQL Object into new class
So I am not sure what your question is now? You can do this:
Or this:
Which one do you want to do?
Code: Select all
class Achievements {
protected $db;
function __construct($host,$user,$password,$db) {
$this->db = new MySQL($host,$user,$password,$db);
}
}
$obj = new Achievements($host,$user,$password,$db);Code: Select all
class Achievements {
protected $db;
function __construct($db) {
$this->db = $db;
}
}
$obj = new Achievements(new MySQL($host,$user,$password,$db));(#10850)
Re: Passing SQL Object into new class
Looks like it's PHP4 and passing an object as a function argument is in fact cloning. Try passing and assigning it by reference:
Code: Select all
class NewClass {
var $db;
function __construct(&$db) {
$this->db = &$db;
}
}There are 10 types of people in this world, those who understand binary and those who don't
Re: Passing SQL Object into new class
Unless it's changed, the code uses __construct as the constructor. That's PHP 5.VladSun wrote:Looks like it's PHP4 and passing an object as a function argument is in fact cloning.
Re: Passing SQL Object into new class
Yes, that's correct. The "var" syntax misled me 
There are 10 types of people in this world, those who understand binary and those who don't
Re: Passing SQL Object into new class
Seriously, why not just make your database class a singleton so you aren't passing whole classes in to other classes as a construct option, then you are minimizing the amount of code you are writing and have way better memory management. Here is an extremely simplified example
There you go, a very over simplified usage of databases in classes without wasting memory passing them in through constructors. This would be the optimal solution
Code: Select all
/*
*Simplified Database singleton class
/*
require_once('config.php');
class DB {
protected static $instance;
public $query;
public static function get() {
if(!isset(self::$instance)) {
self::$instance = new self();
}
return self::$instance;
}
public function __construct() {
$this->connect();
}
public function connect() {
mysql_connect(DB_HOST, DB_USER, DB_PASS);
mysql_select_db(DB);
}
public function execute($sql) {
$this->query = $sql;
if(mysql_query($sql)) {
return true;
} else {
return false;
}
}
}
then config.php...
define('DB_HOST', 'localhost'); //whatever your db connection is
define('DB_USER', 'root'); //your database username
define('DB_PASS', 'admin'); //your db pass
define('DB', 'users'); //whatever your default database is
now whatever class you want to use the database in you can either use it in scope without passing a class through as a reference like so...
class someClass {
public $db;
public function __construct() {
$this->db = DB::get();
}
}
that way you are not passing your class through and you leave passing in variables for your class logic. Or even better ...
class someClass {
public function __construct($var = false) {
if($var) {
$this->retrieve($var);
}
}
public function retrieve($id) {
//use the databse class as it should be used... a singleton
$sql = "SELECT ..." // whatever logic you need to retrieve your info from the database
return DB::get()->fetch_one($sql);
}
}
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Passing SQL Object into new class
No reason other than the standard problems with globals.danwguy wrote:Seriously, why not just make your database class a singleton
(#10850)
Re: Passing SQL Object into new class
+1Christopher wrote:No reason other than the standard problems with globals.danwguy wrote:Seriously, why not just make your database class a singleton
Singletons are used to ensure that there is only one instance of a class. That's why Singleton constructor is a protected method - effectively disabling the "new" operator for its class.
Singletons should never be used as a way for global access to an object instance.
So, again it should be:
Code: Select all
class NewClass {
protected $db;
function __construct($db) {
$this->db = $db;
}
}
$newClass = new NewClass(DB::getInstance());
There are 10 types of people in this world, those who understand binary and those who don't