0

I think the title is right but please correct me if It is mis-leading.

The problem: I have a class which wants to use the DB class, now instead of having to "global $db;" in every method I wish to use the DB object I want to be able to place the object reference in my class properties.

Still following? OK here goes:

class user { private $id = 0; private $name = NULL; private $password = NULL; private $db; function __construct() { $this->load_db(); } private function load_db() { global $db; $this->$db =& $db; } 

I get an error "Object of class db could not be converted to string" which is annoying as I can't figure out how to set the var type in PHP...

Now my question is two fold:

1) How do I fix this. or 2) Is there a better way of doing it as this feels really "kack-handed".

Thanks in advance,

Dorjan

edit: Just to make sure I'm clear I do not want to make multiple instances of the same DB object. At least I believe this to be a good practice ^,^

5
  • At which line do you get that error? Commented Jan 22, 2010 at 15:46
  • I feel so embarrassed. Thanks guys! Commented Jan 22, 2010 at 15:48
  • 2
    Instead of using global $db altogether you might be interested in en.wikipedia.org/wiki/Dependency_injection Can be as simple as public function setDb(PDO $dbObject) { $this->db = db; } Commented Jan 22, 2010 at 15:49
  • AS many of you have given this advice I'll quickly try that. Commented Jan 22, 2010 at 15:50
  • Perfect! Now it looks very clean and avoiding the global :D You guys always make my code that much better! Commented Jan 22, 2010 at 15:51

6 Answers 6

2

If you're using $this, you only need one dollar sign. So $this->db.

Sign up to request clarification or add additional context in comments.

2 Comments

This is the correct answer for 1), but the other answers on dependency injection are good advice for 2).
Agreed! Thanks for answering my main question!
2
private function load_db() { global $db; $this->db =& $db; } 

Also if you are using php 5 you dont need to use the =& operator because objects are implicitly passed by reference. Secondly, you should inject $db into the constructor or fetch it from a Registry object instead of using global.

2 Comments

oh really? = no longer creates a copy but rather points to the original?
Actually: Since PHP 5, new returns a reference automatically, so using =& in this context is deprecated and produces an E_STRICT message. This is on functions and methods not on assigning var's which still need the & to do this... I'm still unsure if what you say is indeed true as I can't find the officially documentation saying that Objects are implicitly passed by reference. I'd love to just trust you but I want to make sure :)
2

You should better do

class User { private $id = 0; private $name = NULL; private $password = NULL; private $db; function __construct($db=null) { $this->db = $db; } } 

Note that you don't put a $ before variable names if you access properties via $object->. That is were the error comes from.

To use a global inside a class method is really bad practice as you somehow tie your class to that global variable. Better pass it as a parameter to the constructor or a method.

Later you can instantiate the class with

$user = new User($db) 

Also note that class names by convention start with a capital letter.

1 Comment

:) Very clear makes my code that much better, thank you Felix.
1

This:

$this->db 

Comments

1

Inject the db instance through the constructor

public function __construct($db) { $this->db = $db; } 

Comments

1

Yopu'll want to use something called dependency injection. This is where you "inject" an object into another object to do whatever it is the object does. In this case do database stuff

class user { private $id = 0; private $name = NULL; private $password = NULL; private $db;

function __construct($database_object) { $this->load_db(); $this->$db = $database_object; } public function do_db_stuff() { $this->$db->doStuff(); } $db = new Mysql() $user = new User($db); $user->do_db_stuff(); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.