Hall of Famer Posted May 17, 2012 Share Posted May 17, 2012 Well in my script exist certain objects being used everywhere, in main script files and class library files. A good example is this Page class: <?php class Page{ public $type; public $name; private $title = ""; private $content = ""; private $date = ""; private $links; private $sidebar; private $ads; public function __construct($page = ""){ // Constructor method of page class } public function gettitle(){ if(empty($this->title)) throw new Exception('The page has no title.'); return $this->title; } public function settitle($title){ $title = secure($title); if(empty($title)) throw new Exception('Cannot set title for this page.'); else $this->title = $title; } public function getcontent(){ if(empty($this->title)) throw new Exception('The page has no content.'); return $this->content; } public function addcontent($content, $overwrite = FALSE){ $content = secure($content); if(empty($this->content) or $overwrite == TRUE) $this->content = $content; else $this->content .= $content; } public function getlinks(){ // the method that grabs user links } public function getsidebar(){ // the method that loads sidebar } public function getads(){ // the method that shows ads } public function display(){ // the method that format pages and brings everything together } } ?> Now lets say I have other classes such as User, Message and Item, a page object will need to be used inside some of their methods so that the page title/content can be modified when necessary. This seems to be quite problematic to me, since I have to declare this Page object global in every method that uses it. Similar problem occurs with my database object, and its even more annoying since almost half of my class methods need to use database queries and commands. So I was wondering... Is there a better way for me to handle these system objects such as Page and Database rather than having to declare them as global inside every single method that deals with them? Please help... Quote Link to comment https://forums.phpfreaks.com/topic/262699-how-do-you-handle-system-objects-used-in-almost-every-other-class/ Share on other sites More sharing options...
trq Posted May 17, 2012 Share Posted May 17, 2012 If you have a class that depends on the Page class, the Page class should be passed in via dependency injection. This means you either pass it into the __construct, or create a specialised method for passing it into the object. Quote Link to comment https://forums.phpfreaks.com/topic/262699-how-do-you-handle-system-objects-used-in-almost-every-other-class/#findComment-1346458 Share on other sites More sharing options...
trq Posted May 17, 2012 Share Posted May 17, 2012 Another option is to look at one of the many "dependency injection" libraries around. Using these, your classes depend on a single "DI Manager" object, which in turn, stores all your dependencies. Proem has one (there is a link in my signature) but there are also numerous stand alone options around. Quote Link to comment https://forums.phpfreaks.com/topic/262699-how-do-you-handle-system-objects-used-in-almost-every-other-class/#findComment-1346459 Share on other sites More sharing options...
Hall of Famer Posted May 18, 2012 Author Share Posted May 18, 2012 Another option is to look at one of the many "dependency injection" libraries around. Using these, your classes depend on a single "DI Manager" object, which in turn, stores all your dependencies. Proem has one (there is a link in my signature) but there are also numerous stand alone options around. Sounds interesting, thanks for the advice. I am not quite sure if Dependency injection will work in this case as the page object is a system utility object whose properties/methods need to be accessible from both inside and outside classes. This is why I've been using global keyword for it, but I am sure there are better ways. Will study more into dependency injection to see if it indeed does the same thing as declaring global more efficiently. Quote Link to comment https://forums.phpfreaks.com/topic/262699-how-do-you-handle-system-objects-used-in-almost-every-other-class/#findComment-1346529 Share on other sites More sharing options...
ignace Posted May 18, 2012 Share Posted May 18, 2012 I like Pimple as a DI container: https://github.com/fabpot/Pimple A use-case can be found at: https://github.com/fabpot/Silex/blob/master/src/Silex/Application.php Quote Link to comment https://forums.phpfreaks.com/topic/262699-how-do-you-handle-system-objects-used-in-almost-every-other-class/#findComment-1346543 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.