![]() |
Super Object - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Super Object (/showthread.php?tid=7547) |
Super Object - El Forum - 04-14-2008 [eluser]Popcorn[/eluser] Hi, I have been using PHP for a number of years now and understand a good amount of OOP (Interfaces, Design Patterns and what not). I wanted to understand how the Super Object class in CodeIgniter works. I know it uses a Singleton design pattern to return an object, but I don't quite understand how the other classes make use of this, since all clases don't have this implementation? I think the database classes, libraries are referenced to the super object and are all accesible from that? I'm not sure. If someone could provide a small example that would be fantastic. Kind Regards, -Mathew Super Object - El Forum - 04-14-2008 [eluser]wiredesignz[/eluser] Yes you are correct, the core libraries become attached as class variables to the CI Super Object. My wiki contribution on Modular Extension has a simple "How it Works" paragraph that may help. Super Object - El Forum - 04-14-2008 [eluser]nmweb[/eluser] The superobject is simply the object all libraries are registered in. <code> class Superobject{} $so=new Superobject; $so->database=new Database(); </code> Since the object is a singleton it can be retrieved from within the Database library for example. The only thing $this->load('database') does is include the file with the Database class, instantiate it and put it in the superobject much alike the example above. Since some libraries gain access to the singleton some circular referencing is happening where the library contains the singleton contains the library etc. Php understands this so it doesn't cause any problems. It is the reason why var_dump($this) can be unreadable ![]() Super Object - El Forum - 04-14-2008 [eluser]halex[/eluser] Quote:It is the reason why var_dump($this) can be unreadableThanks for this! I waste 30 min to try var_dump($this). |