What is $this->variable referenced as?

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
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

What is $this->variable referenced as?

Post by JAB Creations »

What is $this->variable referenced as?
User avatar
panic!
Forum Regular
Posts: 516
Joined: Mon Jul 31, 2006 7:59 am
Location: Brighton, UK

Re: What is $this->variable referenced as?

Post by panic! »

How do you mean?
User avatar
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?

Post by JAB Creations »

$variable

function()

What is $this->variable referenced as?
User avatar
panic!
Forum Regular
Posts: 516
Joined: Mon Jul 31, 2006 7:59 am
Location: Brighton, UK

Re: What is $this->variable referenced as?

Post by panic! »

Are you asking what a variable that belongs to an instance of a class is referred to as?
User avatar
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?

Post 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. :|
Cut
Forum Commoner
Posts: 39
Joined: Sat Aug 23, 2008 8:01 pm

Re: What is $this->variable referenced as?

Post by Cut »

The PHP manual calls it a "pseudo-variable." It means "this class," called from within a class.

http://us.php.net/oop
User avatar
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?

Post 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?
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Re: What is $this->variable referenced as?

Post 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.
User avatar
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?

Post 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?
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Re: What is $this->variable referenced as?

Post 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?
User avatar
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?

Post 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?
User avatar
panic!
Forum Regular
Posts: 516
Joined: Mon Jul 31, 2006 7:59 am
Location: Brighton, UK

Re: What is $this->variable referenced as?

Post 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.
User avatar
panic!
Forum Regular
Posts: 516
Joined: Mon Jul 31, 2006 7:59 am
Location: Brighton, UK

Re: What is $this->variable referenced as?

Post 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.
Post Reply