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

6.4     Variables

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

You've seen that it is quite easy to define functions for your classes, and it is just as easy to define variables. Take a look at this new class definition for our dog class:

class dog {
    public
$Name;

    public function
bark() {
        print
"Woof!\n";
    }
}

The line public $Name; is the key - it defines a public variable called $Name that all objects of class dog will have. PHP allows you to specify how each variable can be accessed, and we will be covering that in depth soon - for now, we will just be using "public".

Author's Note: when designing your own classes, consider putting an underscore in front of object variables so as to distinguish them from parameters passed into an object's functions and also local variables. This is not done here for the sake of maximum readability, plus of course I do not want people who missed this note to think using an underscore is required!

We can now set Poppy's name by using this code:

$poppy->Name = "Poppy";

Notice that -> is used again to work with the object $poppy, and also that there is no dollar before Name. The following would be incorrect:

$poppy->$Name = "Poppy";

PHP's variables have one dollar sign and one only. The reason for this is because the incorrect line of code works like a variable variable - PHP would look up the value of $Name, then try to set that value inside $poppy. The following code would work, but is kind of crazy:

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

Author's Note: Just in case you were skimming over this section and missed out on the warning, PHP's variables have one dollar sign and one only. If you have more, eg $$foo or $foo->$bar, it means something entirely different and is generally left to the realm of advanced programmers.

As mentioned already, each object has its own set of variables that are independent of other objects of the same type. Consider the following code:

$poppy = new poodle;
$penny = new poodle;
$poppy->Name = "Poppy";
$penny->Name = "Penny";
print
$poppy->Name;

That will still output "Poppy", because Penny's variables are separate from Poppy's - hopefully you can now see quite how well OOP models real world scenarios!





<< 6.3 Objects   6.5 The 'this' variable >>
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
Tom - 29 Aug 2008

>$poppy->$Name = "Poppy";
That actually is right, just probably not what most people intend. If you set $Name to 'Name', it'll work.

<?php
$name = 'Name';
$poppy->$name = "Poppy";
?>

That will work, though it's somewhat pointless.



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 seven plus two?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow