Manixat Posted March 24, 2013 Share Posted March 24, 2013 (edited) Hello freaks, I am having quite a difficult time working out how to use an instance of a certain class in all instances of all objects. Example:this is my main file ( index etc. ), <?php $a = new User(); $b = new HeaderBuilder($_SESSION['userid']); ?> HeaderBuilder: <?php class HeaderBuilder{ function __construct($id){ echo $a->getUserById($id); } } Now my question is how do I use $a in HeaderBuilder's __construct without creating a new instance ? Edited March 24, 2013 by Manixat Quote Link to comment https://forums.phpfreaks.com/topic/276090-oop-global-variables/ Share on other sites More sharing options...
ignace Posted March 24, 2013 Share Posted March 24, 2013 Just pass the instance to the HeaderBuilder's constructor. class HeaderBuilder { private $user; public function __construct(User $user) { $this->user = $user; } } Quote Link to comment https://forums.phpfreaks.com/topic/276090-oop-global-variables/#findComment-1420743 Share on other sites More sharing options...
Manixat Posted March 24, 2013 Author Share Posted March 24, 2013 (edited) This is logical but I just want it to be a single variable. Doing that to all my methods where I need user() is going to be a pain in the you know what. I tried with $_GLOBALS and it worked but it is just so long and annoying to type every time. Why can't I just simply assign a global variable in the main file O.o Edited March 24, 2013 by Manixat Quote Link to comment https://forums.phpfreaks.com/topic/276090-oop-global-variables/#findComment-1420746 Share on other sites More sharing options...
Solution Hall of Famer Posted March 24, 2013 Solution Share Posted March 24, 2013 Well good OOP design requires no global variable or at least minimum usage of globals. Technically you can achieve the same functionality as global variables with Singleton/Registry pattern, but these are often considered anti-patterns. Quote Link to comment https://forums.phpfreaks.com/topic/276090-oop-global-variables/#findComment-1420748 Share on other sites More sharing options...
Manixat Posted March 24, 2013 Author Share Posted March 24, 2013 (edited) I have heard that using global variables in OOP is bad, but I just don't see the logic in creating a hundred instances of a class in a single page build when you can ( or I hope I can ) use a global one :/ Edited March 24, 2013 by Manixat Quote Link to comment https://forums.phpfreaks.com/topic/276090-oop-global-variables/#findComment-1420751 Share on other sites More sharing options...
ignace Posted March 24, 2013 Share Posted March 24, 2013 (edited) When you pass a class to another class, you pass the reference to the original instance you don't create a new object. $u = new User('ignace'); $a = new Foo($u); $b = new Foo($u); $c = new Foo($u); $d = new Foo($u); $u->setName('Manixat'); $c->printUserName(); // Manixat $d->printUserName(); // ManixatThis is different from your normal procedural thinking. No matter how you turn it though if your class has a dependency on another class you'll still have to type it. Show us an example of what you mean by that you have to pass/create an instance in other classes. Edited March 24, 2013 by ignace Quote Link to comment https://forums.phpfreaks.com/topic/276090-oop-global-variables/#findComment-1420769 Share on other sites More sharing options...
Manixat Posted March 25, 2013 Author Share Posted March 25, 2013 What I was actually looking for was indeed singleton, I just haven't had read about it in depth and understood it well. Now that I do, it's exactly what I needed, thank you for your replies! Quote Link to comment https://forums.phpfreaks.com/topic/276090-oop-global-variables/#findComment-1420930 Share on other sites More sharing options...
trq Posted March 29, 2013 Share Posted March 29, 2013 Nooo..... singletons are almost always not needed and they make it very easy to make your code tightly coupled. Post what you have. Quote Link to comment https://forums.phpfreaks.com/topic/276090-oop-global-variables/#findComment-1421865 Share on other sites More sharing options...
SurvivorZ Posted March 30, 2013 Share Posted March 30, 2013 I am positive that what you want is a Factory instead of the Singleton. But really, what you want to research is called Constructor-based Dependency Injection. Quote Link to comment https://forums.phpfreaks.com/topic/276090-oop-global-variables/#findComment-1421948 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.