Page 1 of 1

Silly Class Question

Posted: Tue May 25, 2010 5:46 pm
by justme
Nothing in general to my coding but I am learning php and have been working with some frameworks

What striked me as odd was in CI where to load a view you would use $this->load->view('view_to_load'); I was looking at this and noticed its 3 levels from what I can view you have $this which pertains to class (yes|no) ->view which goes to the function (yes|no) and the bit i am confused is the further ->view('view_to_load');

When I have created classes in the past I only had the 2 level $this->view('view_to_load'); but something tells me this looks like a cleaner way of building does anyone have a sample of this 3 level class ????

Thanks...

Re: Silly Class Question

Posted: Tue May 25, 2010 5:52 pm
by John Cartwright
1. $this refers to the class instance of the current scope
2. load is simply a public property of the class of which $this refers to.
3. view() is a method of the load property instance.

So basically your main class holds an instance of some loading class, which has a view method.

Re: Silly Class Question

Posted: Tue May 25, 2010 7:14 pm
by justme
so just so i can get a hang on it would it be like this

Code: Select all

class Main
{
          function load()
          {
          if($toBe || $notToBe) print "That is the question";
          }
}

$main = new Main;

$main->load->toBe('some_old_crap');
sorry if that sounds dumb just trying to get to grips with it all

Re: Silly Class Question

Posted: Tue May 25, 2010 7:59 pm
by Jonah Bron
Like this:

Code: Select all

class SomeClass {
    function view($var){
        // do something
    }
}

class SomeOtherClass {
    $load = new SomeClass();
    function somefunction() {
        $this->load->view('view_to_load');
    }
}
So, $this is a reference to itself (SomeOtherClass, since that's the class it's in), load is a property of self (load is defined as a new instance of SomeClass), which holds the function view().

You might have to think about it for a minute :wink:

Re: Silly Class Question

Posted: Tue May 25, 2010 8:02 pm
by Eran
'$this' is a reference to the object instance of the class (object scope). 'self' is a reference to the actual class (static class scope).

Re: Silly Class Question

Posted: Tue May 25, 2010 8:21 pm
by Jonah Bron
Did I make a boo boo? Was that a correction or an addition?

Re: Silly Class Question

Posted: Wed May 26, 2010 12:47 am
by Eran
Probably precision. '$this' is not a reference to the class but to the object instance. This is not a semantic difference either, since there is a class scope, which is static, and referred to by 'self'.

Re: Silly Class Question

Posted: Wed May 26, 2010 2:08 am
by Christopher