Welcome Guest, Not a member yet? Register   Sign In
Super Object
#1

[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
#2

[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.
#3

[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 Wink
#4

[eluser]halex[/eluser]
Quote:It is the reason why var_dump($this) can be unreadable
Thanks for this! I waste 30 min to try var_dump($this).




Theme © iAndrew 2016 - Forum software by © MyBB