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

6.7.3     Protected

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

Variables and functions marked as protected are accessible only through the object that owns them, whether or not they are declared in that object's class or whether they have descended from a parent class. Consider the following code:

<?php
    
class dog {
        public
$Name;
        private function
getName() {
            return
$this->Name;
        }
    }

    class
poodle extends dog {
        public function
bark() {
            print
"'Woof', says " . $this->getName();
        }
    }
    
    
$poppy = new poodle;
    
$poppy->Name = "Poppy";
    
$poppy->bark();
?>

In that code, the class poodle extends from class dog, class dog has a public variable $Name and a private function getName(), and class poodle has a public function called bark(). So, we create a poodle, give it a $Name value of "Poppy" (the $Name variable comes from the dog class), then ask it to bark(). The bark() function is public, which means we can call it as shown above, so this is all well and good.

However, note that the bark() function calls the getName() function, which is part of the dog class and was marked private - this will stop the script from working, because private variables and functions cannot be accessed from inherited classes. That is, we cannot access private dog functions and variables from inside the poodle class.

Now try changing bark() to protected, and all should become clear - the variable is still not available to the world as a whole, but handles inheritance as you would expect, which means that we can access getName() from inside poodle.





<< 6.7.2 Private   6.7.4 Final >>
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
A PHP User - 05 Dec 2008

Yea, someone should report that bug.

Vbabiy - 05 Dec 2008

That is correct you need to change getname() to protected

chima/chytons@yahoo.com - 05 Dec 2008

JJKola, I agree with you.

Unix Programmer - 05 Dec 2008

In the last paragraph, you might also want to change "the variable is still not available" to "the function is still not available".

(Really nice explanation!)

JJkola - 05 Dec 2008

I think you should change "Now try changing bark() to protected" to "Now try changing getName() to protected" because otherwise it will not help situation.



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


Top-right shadow
 
Bottom-left shadow Bottom shadow