Hudzilla.org - the homepage of Paul Hudson
Contents > Objects Wish List | Report Bug | About Me ]

6.5     The 'this' variable

This is NOT the latest copy of this book; click here for the latest version.

Once inside an object's function, you have complete access to its variables, but to set them you need to be more specific than just using the variable name you want to work with. To properly specify you want to work with a local variable, you need to use the special $this variable, which PHP always sets to point to the object you are currently working with.

For example:

function bark() {
    print
"{$this->Name} says Woof!\n";
}

Whenever you are inside a function of an object, PHP automatically sets the $this variable contains that object - you do not need to do anything to have access to it.





<< 6.4 Variables   6.6 Objects within objects >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
Steve A. - 30 Aug 2008

May 25, 2006

Oops. Obviously in the previous post I forgot the

<?php

Also, I've been wondering why I didn't have to declare the $Name variable in the class defintion for this to work. Does $poppy->Name="Poppy"; create the $Name variable?

Steve A. - 30 Aug 2008

Scot, this works for me in PHP 5:

class dog {
public function bark() {
print "{$this->Name} says Woof!";
}
}

class poodle extends dog {

}

$poppy = new poodle;
$poppy->Name="Poppy";

$poppy->bark();

?>


Outputs "Poppy says woof!"

ScotDiddle@overnite.com - 30 Aug 2008

I could not get this example to work. Please clarify. Thank-you. Scot L. Diddle, Richmond VA

micah.frost@gmail.com - 30 Aug 2008

" Whenever you are inside a function of an object, PHP automatically sets the $this variable contains that object - you do not need to do anything to have access to it."

Whenever you are calling an object's function, the $this variable is set to the object whose function is being called.

chris.hearn01@ntlworld.com - 30 Aug 2008

Hi there,
Having been involved in other things for a while, just came back to reading your book a bit more.
Two quick points.
a)
" Whenever you are inside a function of an object, PHP automatically sets the $this variable contains that object - you do not need to do anything to have access to it."

Sorry, this is not English - or at least, not to me - what does it mean?
(i think it's saying when inside a function of an object, $This-> refers to the object?)

b) it would be helpful to have next page/last page links at the TOP of each page, as i find when scanning through the pages I have to move to the bottom each time. (And maybe a next/last chapter link as well??)

Still think it's a great on-line book though!
Cheers
Chris



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is eight plus nine?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow