Private and protected variables in PHP 5?

From: Date: Fri, 11 Jul 2003 21:21:54 +0000
Subject: Private and protected variables in PHP 5?
Groups: php.general 
Request: Send a blank email to php-general+get-154934@lists.php.net to get a copy of this message
All, I'm toying with the new stuff available in PHP 5 (latest CVS), but I've hit a brick wall: both private and protected don't seem to work as I'd expect them to. Here's an example script: <?php class dog { // declare two private variables private $Name; private $DogTag; public function bark() { print "Woof!\n"; } } // new class, for testing derived stuff class poodle extends dog { public function bark() { print "Yip!\n"; } } // I now create an instance of the // derived class $poppy = new poodle; // and set its private property $poppy->Name = "Poppy"; print $poppy->Name; ?> For some reason, that script works fine - PHP doesn't object to me setting private variables in the derived class. Yet if I use "$poppy = new dog", the script errors out as expected. It's almost like PHP inherits the member variables, but not the attached access control. For protected, here's another script: <?php class dog { // this next function is protected // viz, it should be available to dog // and its children protected function bark() { print "Woof!\n"; } } class poodle extends dog { // nothing happening here } $mydog = new poodle; // I now call the protected function $mydog->bark(); ?> That script errors out saying that I can't call the protected function bark - surely, being protected, it should be available in the poodle class too? Of course, it might be that these two pieces of functionality are not yet implemented in PHP, or, more likely, that I'm just misinterpreting the documentation! ;) If you have any insight, please CC me into your response to the list. Thanks, Paul

« previous php.general (#154934) next »