Php oop call a class within a class?

Unlike C global variables in PHP are not in the visibility scope of functions/methods, see docs.php.net/language.variables.scope To access global variables you must either import them into the function's scope via global $var or acccess them through the superglobal $_GLOBAL.

Unlike C global variables in PHP are not in the visibility scope of functions/methods, see docs.php.net/language.variables.scope To access global variables you must either import them into the function's scope via global $var or acccess them through the superglobal $_GLOBAL But instead of using global variables you might be interested in dependency injection, e.g. (one of many possible ways to implement di): connect(); $main = new main($db); $result = $main->test(); class db { function connect(){ //db connection code here } function query($sql){ //perform query and return results } } class main { protected $db; public function __construct($db) { $this->db = $db; } public function test(){ $sql = "select * from user"; return $this->db->query($sql); } }.

– VolkerK Sep 24 '11 at 14:25 see my commit message. To do DI you do not need a Registry. You just inject the Dependency.

Under the premise that the simplest example is the best example, I felt it would be easier to illustrate the point without adding a new concept to the answer (Registry), especially since Registries violate Law of Demeter and are somewhat controversial regarding DI because you cannot see from the method signature what the consuming class needs in order to run. Registries are kinda like globals in their own scope. Feel free to rollback though.

– Gordon Sep 24 '11 at 14:46 Thanks guys! This worked perfectly. – user962449 Sep 24 '11 at 17:54.

I would add a static getInstance() method into the Db class. Then cou can call Db::getInstance()->query(...) from every scope. You can read more on this pattern on Wikipedia.

Adding a static method getInstance doesn't make a class a Singleton. You need additional boilderplate code for that. And Singletons should be avoided anyway – Gordon Sep 24 '11 at 10:58.

Just put: global $db; in your test() function. But if you do this also make sure you check that $db exists and is what you think it is before you use it this way.

Thanks for your help! I've been up all night trying to figure this out, it's almost 3am here >Or is there a better way? – user962449 Sep 24 '11 at 9:57 @user962449 see the upvoted answer.

Dependency injection. Create your DB object and pass it to the constructor of the object where you wish to use it. – Erik Sep 24 '11 at 10:02 Just for the record, this works and is quick.

He didn't ask for the best solution, just something to get the code working, and this does that. – Doug Treadwell Sep 24 '11 at 10:20.

I guess you should be using $db = new db; because $db is not defined in your main class.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions