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

6.7.1     Public

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

Public variables and functions are accessible from anywhere in your script, which makes this modifier the easiest to use. In PHP 4, all object variables were declared with "var" and were essentially public, but using this terminology is deprecated and may generate compiler warnings. Take a look at this following code:

<?php
    
class dog {
        public
$Name;

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

    class
poodle extends dog {
        public function
bark() {
            print
"Yip!\n";
        }
    }

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

If you try that code out, you will see it works in precisely the same way as before - the public keyword does not make any difference. The reason behind is that, by default, all class functions are public, because before PHP 5 there was no way to make them anything else.

While the public keyword is not needed, I recommend you use it anyway - it is a good way to remind people who read your code that a given function is indeed public, and also it is possible that class functions without an access modifier may be deprecated in the future.

When you use public for variables, it is needed - you always need to specify an access modifier for variables, because otherwise there'd be no way to define what variables a class has. Previous versions of PHP used the "var" keyword to declare class variables, again because it had no concept of access modifiers - you should avoid this, and be more specific with public or one of the other keywords.





<< 6.7 Access control modifiers   6.7.2 Private >>
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
Be the first to add a comment to this chapter!



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


Top-right shadow
 
Bottom-left shadow Bottom shadow