Page 1 of 1
What is $this->variable referenced as?
Posted: Sat Sep 06, 2008 5:33 am
by JAB Creations
What is $this->variable referenced as?
Re: What is $this->variable referenced as?
Posted: Sat Sep 06, 2008 9:57 am
by panic!
How do you mean?
Re: What is $this->variable referenced as?
Posted: Sat Sep 06, 2008 9:58 am
by JAB Creations
$variable
function()
What is $this->variable referenced as?
Re: What is $this->variable referenced as?
Posted: Sat Sep 06, 2008 10:16 am
by panic!
Are you asking what a variable that belongs to an instance of a class is referred to as?
Re: What is $this->variable referenced as?
Posted: Sat Sep 06, 2008 11:44 am
by JAB Creations
It's a (WHAT) variable?
A
pointer variable? Obviously the wrong terminology.
If I don't know how to reference it then I can't look it up and I see this used all over the place.

Re: What is $this->variable referenced as?
Posted: Sat Sep 06, 2008 12:57 pm
by Cut
The PHP manual calls it a "pseudo-variable." It means "this class," called from within a class.
http://us.php.net/oop
Re: What is $this->variable referenced as?
Posted: Sat Sep 06, 2008 9:37 pm
by JAB Creations
I've done a bit more reading but the question that now eludes me is why? Why use a pseudo-variable instead of a regular variable?
Re: What is $this->variable referenced as?
Posted: Sun Sep 07, 2008 8:31 am
by markusn00b
JAB Creations wrote:I've done a bit more reading but the question that now eludes me is why? Why use a pseudo-variable instead of a regular variable?
Do not question PHP!
Does it really matter
why it's like this?
Plus, it's not only PHP; most OOP languages use the 'this' keyword.
Re: What is $this->variable referenced as?
Posted: Sun Sep 07, 2008 11:03 am
by JAB Creations
If I don't know why it's good to use it then I won't understand what it's intended to be used for! What is the benefit of using pseudo-variable instead of a regular variable? What can you do with a pseudo-variable that you can't with a regular variable?
Re: What is $this->variable referenced as?
Posted: Tue Sep 09, 2008 8:13 am
by markusn00b
JAB Creations wrote:If I don't know why it's good to use it then I won't understand what it's intended to be used for! What is the benefit of using pseudo-variable instead of a regular variable? What can you do with a pseudo-variable that you can't with a regular variable?
Well you
can't access a variable inside a class without
$this->, so I guess that is good enough reason to use it?
Re: What is $this->variable referenced as?
Posted: Tue Sep 09, 2008 8:20 am
by JAB Creations
Then building on top of that I have been told you can use a class in any file that is included...thinking that you couldn't use variables in the same way which you actually can (just so long as they are not declared within a function). So can you use a class declared outside a function within the function or would you have to pass it as a parameter?
I also have a very vague concept of what classes are useful for and how without classes other parts of the language would have to be put in to an extensive function (of sorts) to achieve class like functionality...whatever it may be.
The big question is why is the given PHP syntax useful which can also be translated in to what is simplified that if absent would be difficult to program in PHP?
Re: What is $this->variable referenced as?
Posted: Tue Sep 09, 2008 8:27 am
by panic!
Code: Select all
class cat
{
public $age;
function __construct()
{
$this->age=100; // you can get this later
$age=100; //this will get lost
}
function age()
{
print $this->age; // prints 100
print $age; /// uh oh! Notice (if you have them switched on
}
}
$cat=new cat();
$cat->age();
You use $this to reference properties of a class because variables within a method are lost outside the scope of that specific instance of the method.
Re: What is $this->variable referenced as?
Posted: Tue Sep 09, 2008 8:30 am
by panic!
P.S:
I couldn't use PHP without classes. You are a victim of the fact that PHP is 99% taught with Object Oriented aspect of PHP an afterthought and a novel concept, when it's so, so important and should be introduced almost from day one.