Page 2 of 2

Re: Passing SQL Object into new class

Posted: Sat Nov 10, 2012 10:10 pm
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.

Re: Passing SQL Object into new class

Posted: Sun Dec 02, 2012 2:29 pm
by pinehead
Positive i've showed you the final code.
and the changes i made.

Re: Passing SQL Object into new class

Posted: Sun Dec 02, 2012 9:59 pm
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?

Re: Passing SQL Object into new class

Posted: Sat Dec 08, 2012 4:52 pm
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;
  }

}

Re: Passing SQL Object into new class

Posted: Sat Dec 08, 2012 5:13 pm
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.

Re: Passing SQL Object into new class

Posted: Mon Dec 10, 2012 2:34 am
by VladSun
Yes, that's correct. The "var" syntax misled me :)

Re: Passing SQL Object into new class

Posted: Sat Dec 15, 2012 9:27 pm
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

Re: Passing SQL Object into new class

Posted: Sat Dec 15, 2012 10:26 pm
by Christopher
danwguy wrote:Seriously, why not just make your database class a singleton
No reason other than the standard problems with globals.

Re: Passing SQL Object into new class

Posted: Sun Dec 16, 2012 6:06 am
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());