Objects in PHP including other objects -


I think the title is correct, but please correct me if it is wrong-headed.

Problem: I have a class that wants to use DB class, instead of being "global $ db"; In every method I want to use the DB object, I want to be able to set object references in my class properties.

Still the following? Ok here:

  class user {personal $ id = 0; Private $ name = NULL; Private $ password = NULL; Private $ db; Function __ conversion () {$ this- & gt; Load_db (); } Private function load_db () {global $ db; $ This- & gt; $ Db = & amp; $ Db; }  

I get an error "Class DB can not be converted to object string" which is disturbed because I do not know how to set var type in PHP. .

Now my question is two times:

1) How can I fix it or 2) Is there a better way to do this because it is actually "cake-hand "Feels.

Thanks Dorjan

Edit: Just to make sure that I'm clear that I do not create many examples of the same DB object Want to At least I believe that this is a good practice ^, ^

If you have $ $ , You should only indicate a dollar if $ this-> Db .


Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -