Silly Class Question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
justme
Forum Newbie
Posts: 2
Joined: Tue May 25, 2010 5:37 pm

Silly Class Question

Post 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...
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Silly Class Question

Post 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.
justme
Forum Newbie
Posts: 2
Joined: Tue May 25, 2010 5:37 pm

Re: Silly Class Question

Post 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
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Silly Class Question

Post 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:
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Silly Class Question

Post 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).
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Silly Class Question

Post by Jonah Bron »

Did I make a boo boo? Was that a correction or an addition?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Silly Class Question

Post 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'.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Silly Class Question

Post by Christopher »

(#10850)
Post Reply