ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

nested functions/classes

November 05, 2010 3:48am

Subscribe [2]
  • #1 / Nov 05, 2010 3:48am

    vtwebtech

    2 posts

    I see that when you “load” a view you can call something like $this -> load -> view();, but you can’t nest classes or functions in php, so how do you do that?

  • #2 / Nov 05, 2010 4:48am

    davidbehler

    708 posts

    That’s pretty simple:

    “$this” refers to the controller object itself (you would be calling $this->load->view() in one of the methods of a controller)

    “load” is a property of the controller (inherited from the CI_Controller class) which holds an instance of the loader library

    “view()” is a method of the loader class (one of the many libraries CI brings along) which can then be called to load a view file

    Hope that helps

  • #3 / Nov 05, 2010 4:48am

    WanWizard

    4475 posts

    You’re not nesting, you’re just calling.

  • #4 / Nov 05, 2010 6:05am

    vtwebtech

    2 posts

    what I’m confused about, is when I type in $this, which of course calls the current class, then the only way I know to call a function is -> my_function();. If I my_function using $this -> anything -> my_function(); then it fails. I feel like an idiot, because I don’t understand what the loader property is. I looked at object properties in the php.net manual, and it made no reference to any such thing, so I am still confused about load.

    Also my goal is to understand this because I am used to being able to use a tree of instructions to define what i want to do, for instance much like ci has done, one would use something.get_object(‘name_of_object’).set_text(something.get_object(‘name_of_object’).get_text);, or computer.screen.width.set_width(’‘);.

    I also know you can $an_object= new your_class; and then call using &an;_object -> your_function(); but this doesn’t lead me to any understanding of what could be put in between.


    I guess my question is how did ci make a hierarchical structure for calling a function. I know that the ci_loader is included in the ci libraries, but I want to know how to do it outside of those libraries.

    Thank you both for trying to answer my question, I’ve only been writing php for three weeks, so I’m pretty new to this, and not just a little confused.

  • #5 / Nov 05, 2010 5:30pm

    WanWizard

    4475 posts

    This is called object chaining.

    $this is the current object. The object can have methods and properties (class variables). When the CI loader class is loaded, an object is created from that class, and this object is stored in the ‘load’ property of the current object.

    So now you have $this->load, which is the loader class object. This object contains methods as well, which you call by using $this->load->method(). In PHP5 you can chain an entire list of objects together. You can do this by making sure your method returns an object.

    A very good example is CI’s active record:

    $result = $this->db->select('fields')->where('field', 'value')->order_by('field')->get();
  • #6 / Nov 05, 2010 6:16pm

    vtwebtech

    2 posts

    Thank you, I was fairly confused, and I didn’t even know what term to search for. That is exactly what I was wondering, they did a good job of it here, but I couldn’t find it many other places, so I was confused. Thank you again, this helps so much.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases