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...
Silly Class Question
Moderator: General Moderators
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Re: Silly Class Question
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.
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
so just so i can get a hang on it would it be like this
sorry if that sounds dumb just trying to get to grips with it all
Code: Select all
class Main
{
function load()
{
if($toBe || $notToBe) print "That is the question";
}
}
$main = new Main;
$main->load->toBe('some_old_crap');
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
Re: Silly Class Question
Like this:
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
Code: Select all
class SomeClass {
function view($var){
// do something
}
}
class SomeOtherClass {
$load = new SomeClass();
function somefunction() {
$this->load->view('view_to_load');
}
}You might have to think about it for a minute
Re: Silly Class Question
'$this' is a reference to the object instance of the class (object scope). 'self' is a reference to the actual class (static class scope).
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
Re: Silly Class Question
Did I make a boo boo? Was that a correction or an addition?
Re: Silly Class Question
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'.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Silly Class Question
For even more nuance: http://www.php.net/manual/en/language.o ... rences.php
(#10850)