Passing SQL Object into new 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

User avatar
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

Post by Christopher »

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)
pinehead
Forum Newbie
Posts: 14
Joined: Sat Oct 13, 2012 11:51 am

Re: Passing SQL Object into new class

Post by pinehead »

Positive i've showed you the final code.
and the changes i made.
User avatar
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

Post by Christopher »

So I am not sure what your question is now? You can do this:

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);
Or this:

Code: Select all

class Achievements {
	protected $db;
	
	function __construct($db) {
	   $this->db = $db;
	}
}
$obj = new Achievements(new MySQL($host,$user,$password,$db));
Which one do you want to do?
(#10850)
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Passing SQL Object into new class

Post by VladSun »

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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Passing SQL Object into new class

Post by requinix »

VladSun wrote:Looks like it's PHP4 and passing an object as a function argument is in fact cloning.
Unless it's changed, the code uses __construct as the constructor. That's PHP 5.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Passing SQL Object into new class

Post by VladSun »

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
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: Passing SQL Object into new class

Post by danwguy »

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

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);
    }
    }
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
User avatar
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

Post by Christopher »

danwguy wrote:Seriously, why not just make your database class a singleton
No reason other than the standard problems with globals.
(#10850)
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Passing SQL Object into new class

Post by VladSun »

Christopher wrote:
danwguy wrote:Seriously, why not just make your database class a singleton
No reason other than the standard problems with globals.
+1

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
Post Reply