What is $this->variable referenced as?
Moderator: General Moderators
- JAB Creations
- DevNet Resident
- Posts: 2341
- Joined: Thu Jan 13, 2005 6:44 pm
- Location: Sarasota Florida
- Contact:
What is $this->variable referenced as?
What is $this->variable referenced as?
Re: What is $this->variable referenced as?
How do you mean?
- JAB Creations
- DevNet Resident
- Posts: 2341
- Joined: Thu Jan 13, 2005 6:44 pm
- Location: Sarasota Florida
- Contact:
Re: What is $this->variable referenced as?
$variable
function()
What is $this->variable referenced as?
function()
What is $this->variable referenced as?
Re: What is $this->variable referenced as?
Are you asking what a variable that belongs to an instance of a class is referred to as?
- JAB Creations
- DevNet Resident
- Posts: 2341
- Joined: Thu Jan 13, 2005 6:44 pm
- Location: Sarasota Florida
- Contact:
Re: What is $this->variable referenced as?
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.
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?
The PHP manual calls it a "pseudo-variable." It means "this class," called from within a class.
http://us.php.net/oop
http://us.php.net/oop
- JAB Creations
- DevNet Resident
- Posts: 2341
- Joined: Thu Jan 13, 2005 6:44 pm
- Location: Sarasota Florida
- Contact:
Re: What is $this->variable referenced as?
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?
- markusn00b
- Forum Contributor
- Posts: 298
- Joined: Sat Oct 20, 2007 2:16 pm
- Location: York, England
Re: What is $this->variable referenced as?
Do not question PHP!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?
Does it really matter why it's like this?
Plus, it's not only PHP; most OOP languages use the 'this' keyword.
- JAB Creations
- DevNet Resident
- Posts: 2341
- Joined: Thu Jan 13, 2005 6:44 pm
- Location: Sarasota Florida
- Contact:
Re: What is $this->variable referenced as?
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?
- markusn00b
- Forum Contributor
- Posts: 298
- Joined: Sat Oct 20, 2007 2:16 pm
- Location: York, England
Re: What is $this->variable referenced as?
Well you can't access a variable inside a class without $this->, so I guess that is good enough reason to use it?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?
- JAB Creations
- DevNet Resident
- Posts: 2341
- Joined: Thu Jan 13, 2005 6:44 pm
- Location: Sarasota Florida
- Contact:
Re: What is $this->variable referenced as?
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?
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?
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?
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.
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.