Hi,
Im just learning OO in PHP and not sure the best method to use the functions for example in a database wrapper class within another class for example which handles all user authentication. I have put together this test code of how I am currently implementing it:
Is that use of global considered bad practice? I was browsing the source of PHPBB3 and they use the global method when accessing its database class from other classes however I believe I can instead pass a reference to the object via a param so would that method be better? If someone could give me a few pointers it would be appreciated.
Cheers.
Im just learning OO in PHP and not sure the best method to use the functions for example in a database wrapper class within another class for example which handles all user authentication. I have put together this test code of how I am currently implementing it:
Code:
<? class DB { public function __construct($host,$user,$pass,$dbname) { mysql_connect($host,$user,$pass) or die(mysql_error()); mysql_select_db($dbname) or die(mysql_error()); } public function qry($sql) { return mysql_query($sql) or die(mysql_error()); } } class User { public $user_id; public function __construct($user_id) { $this->user_id = $user_id; } public function update_user() { global $db; $db->qry(" UPDATE users SET active = 1 WHERE user_id = '" . $this->user_id . "' "); } } $db = new DB("localhost","root","","test_database"); $user = new User(2); $user->update_user(); ?>
Cheers.
Comment