Welcome Guest, Not a member yet? Register   Sign In
[HOW] CI Works
#1

[eluser]4ever[/eluser]
I read the CodeIgniter Reactor prolog but still it's not clear to me where place this topic. What is Core branch and what is primary branch not clear to me.

So I'd like to start discussion about such basic thing - to clarify things - how the CodeIgniter Core works. Well it seems to me, that here is not many people who really understands how the app works, because I had some question that nobody answered to me. So I start reading core code and trying to understand why some things works like so. So here I want to place the questions about loader, etc...

All users are welcome to intervene.
#2

[eluser]n0xie[/eluser]
Did you read http://codeigniter.com/news/codeigniter_2.0.0_released/ ??
#3

[eluser]4ever[/eluser]
I am interested about how the loading process works and how the super object is created.

I am in system\libraries folder now and have open common.php (not a class, note)

The load_class global scope function:

&load;_class - write it without semicolon! This is mistake in filter
Code:
function &load;_class($class, $directory = 'libraries', $prefix = 'CI_')
{
    static $_classes = array();

    // Does the class exist?  If so, we're done...
    if (isset($_classes[$class]))
    {
        return $_classes[$class];
    }

    $name = FALSE;

    // Look for the class first in the native system/libraries folder
    // thenin the local application/libraries folder
    foreach (array(BASEPATH, APPPATH) as $path)
    {
        if (file_exists($path.$directory.'/'.$class.EXT))
        {
            $name = $prefix.$class;

            if (class_exists($name) === FALSE)
            {
                require($path.$directory.'/'.$class.EXT);
            }

            break;
        }
    }

    // Is the request a class extension?  If so we load it too
    if (file_exists(APPPATH.$directory.'/'.config_item('subclass_prefix').$class.EXT))
    {
        $name = config_item('subclass_prefix').$class;

        if (class_exists($name) === FALSE)
        {
            require(APPPATH.$directory.'/'.config_item('subclass_prefix').$class.EXT);
        }
    }

    // Did we find the class?
    if ($name === FALSE)
    {
        // Note: We use exit() rather then show_error() in order to avoid a
        // self-referencing loop with the Excptions class
        exit('Unable to locate the specified class: '.$class.EXT);
    }

    // Keep track of what we just loaded
    is_loaded($class);

    $_classes[$class] = new $name();
    return $_classes[$class];
}

As far as I understand to this class:
(L means line)
L115: arguments to specify class and its location... in function load_class
L120-121: check if the class is loaded
L129: search the directories (BASEPATH, APPATH) if the class exists and if so then require it.

L145:"Is the request a class extension? If so we load it"
Well this surprises me coz I thought that if I have MY_class so it excludes the native class and loads the mine one.
Q: So does it mean that this loads MY_class to addition (as extension)to the native class?
A: Yes

L156 If the class name wasn't found then the class does't exists and return false.

L164: is_loaded($class);
Q: What is that? Does it set information whether it is loaded?
A: Yes it saves.
Q: And is so where does it save?
A: static $_is_loaded ... it is static variable in the function keeping the value to next call

L167: return $_classes[$class];
Q: What type is it returned?
A: It is object.
Q: how to access this object from outside if it is saved?

Q:_classes and _is_loaded
Where they are saved? Under CI super object?
A: these are static values in functions /not class/. They are saved in the function.
Q: Is it possible to access them else way then by the function call? /to call the function where the static is declared/
#4

[eluser]InsiteFX[/eluser]
Look at the CodeIgniter and Loader Classes then you will see what is happening.

InsiteFX
#5

[eluser]4ever[/eluser]
[quote author="InsiteFX" date="1306162863"]Look at the CodeIgniter and Loader Classes then you will see what is happening.

InsiteFX[/quote]

Thanx for response. Yes that is what I did.

Loader uses load_class on line 188, same as CodeIgniter on line 133 etc.

I would like to speak about the load_class to clarify things...

I study the loader now and would like to continue if all would be clear to me about load_class.
#6

[eluser]n0xie[/eluser]
Quote:L145:“Is the request a class extension? If so we load it”
Well this surprises me coz I thought that if I have MY_class so it excludes the native class and loads the mine one. So does it mean that this loads MY_class to addition (as extension)to the native class?
It extends the base CI_ class with the MY_ class. So yes.

Quote:L164: is_loaded($class); // What is that? Does it set information whether it is loaded? And is so where does it save?
It saves state about what classes /libraries are loaded. See the function is_loaded().

Quote:L167: return $_classes[$class];
What type is it returned? Is it object? Or is it array with list of loaded classes?
$_classes is a static array of all the loaded classes. Itś an array where each item in the array is an instantiated class. This code return the instantiated object from that array with as key the name of the class.

Any more questions?
#7

[eluser]4ever[/eluser]
Quote:L164: is_loaded($class);
It saves state about what classes /libraries are loaded. See the function is_loaded().

is_loaded - yes I have seen it. I was not sure with the static declaration in function.
Code:
static $_is_loaded = array();
I don't understand why they didn't place this declaration to class not into the method. I would do it so and accessed like so:
$this->_is_loaded[strtolower($class)] = $class;
So does it mean they created property to the class this way? With static declaration in function?

Quote:$_classes is a static array of all the loaded classes. Itś an array where each item in the array is an instantiated class. This code return the instantiated object from that array with as key the name of the class.

"$_classes is a static array of all the loaded classes."

I understand. Every item in the array is string, right?

"Itś an array where each item in the array is an instantiated class."
and the word "instantiated" means, that the class was loaded and that there is an instance... Object was created. Do I say it right?

"This code return the instantiated object from that array with as key the name of the class."

But I thought that "instantiated object" is the object placed under super object "CI->instantiated object" ... so now I am confused.
#8

[eluser]n0xie[/eluser]
Quote:I don't understand why they didn't place this declaration to class not into the method. I would do it so and accessed like so:
$this->_is_loaded[strtolower($class)] = $class;
Because it's a globally scoped function, not a class property.

Quote:I understand. Every item in the array is string, right?
No every item is an object

Quote:"Itś an array where each item in the array is an instantiated class."
and the word "instantiated" means, that the class was loaded and that there is an instance... Object was created. Do I say it right?
Yes.

Quote:
"This code return the instantiated object from that array with as key the name of the class."

But I thought that "instantiated object" is the object placed under super object "CI->instantiated object" ... so now I am confused.
Not all classes that are loaded are accessible through the CI superobject. Hence the difference
#9

[eluser]4ever[/eluser]
[quote author="n0xie" date="1306169312"]
Quote:I don't understand why they didn't place this declaration to class not into the method. I would do it so and accessed like so:
$this->_is_loaded[strtolower($class)] = $class;
Because it's a globally scoped function, not a class property.
[/quote]

Oh. I didn't see. Common.php isn't class.

[quote author="n0xie" date="1306169312"]
Quote:I understand. Every item ($_classes) in the array is string, right?
No every item is an object
[/quote]
I see now L166:
Code:
$_classes[$class] = new $name();

OK. So new object with class name $name is added to the array $_classes.
And then it is returned L167. But this object is not accessible from super object. Hence it is returned by function load_class (common.php).

Code:
return $_classes[$class];

That is the &load;_class. Clear.

[quote author="n0xie" date="1306169312"]Not all classes that are loaded are accessible through the CI superobject. Hence the difference[/quote]
I guess I should to see the difference between $_classes in global scoped function and $ci_classes in super object. In loader.php is used $ci_classes, in common.php is used $_classes...
#10

[eluser]4ever[/eluser]
For me now it is important to learn about the static keyword because I don't understand how it works. I will learn and come back later to continue about the loader class. Many thanks for now...




Theme © iAndrew 2016 - Forum software by © MyBB