Welcome Guest, Not a member yet? Register   Sign In
CI Superobject
#1

[eluser]Unknown[/eluser]
Hello

This question if for some of the advanced PHP/CodeIgniter users who know something about the code behind.

How does the CodeIgniter uses a Superobject to access all other instantiated classes from?
I've examined the source code multiple times but i can't make a clear view out of it.

Can somebody explain the theory with an example?


Thanks a lot
#2

[eluser]Phil Sturgeon[/eluser]
Take a look at the application flow for starters.

It basically starts at index.php which sets a few constants and includes system/codeigniter/CodeIgniter.php. This is where most of the magic happens.

This controls the order in which everything is loaded and loads many of the components that eventually end up part of the super-object.

The Controller class is then loaded and in the constructor it goes through an array of classes which it would like to make part of $this.

Code:
$classes = array(
                            'config'    => 'Config',
                            'input'        => 'Input',
                            'benchmark'    => 'Benchmark',
                            'uri'        => 'URI',
                            'output'    => 'Output',
                            'lang'        => 'Language',
                            'router'    => 'Router'
                            );

Then it does a few things like $this->load =& load_class('Loader'); and the super-global is ready!

It is available in your controllers because they extend from the main controller class. They are available in views as views are just included.

In libraries and models it is a little different as $this reffers to the library or model, which is why you need to use $CI =& get_instance(); get_instance() is a function that returns the instance based on some very clever OOP trickery in CI_Base, which is what Controller extends from.

It is all very clever you see. Very clever! :-)
#3

[eluser]jedd[/eluser]
You can also crank up xdebug, and watch its output - primarily designed to show what's eating up your space, but also very good for showing what files are being loaded in sequence.




Theme © iAndrew 2016 - Forum software by © MyBB