Re: Fwd: Re: [PHP] Private and protected variables in PHP 5?

From: Date: Sat, 12 Jul 2003 17:22:04 +0000
Subject: Re: Fwd: Re: [PHP] Private and protected variables in PHP 5?
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to internals+get-3389@lists.php.net to get a copy of this message
Setting a private error does error out if it's in the hierarchy of the current class, i.e. if you'd try setting Name on an object of type dog. However, if you inherit from this class the new class does not know any of the parents' private members and therefore doesn't error out. This is the correct way of handling it because private members should be hidden to inheriting classes. Andi At 05:10 PM 12/7/2003 +0100, Paul Hudson wrote:
Alan, I think what you are getting at is that you can set 'private' variable from outside.. - with no warnings etc. Yup. so if you want a warning when you set/create a variable with the same name as the private - use protected... Or am I missing the point on what you expected..? I was expecting setting a private variable to error out. Protected works as you say - it errors out as expected. But surely private should error also - inherited classes inherit private variables, but they can't manipulate them (or at least so I thought). This code: <?php class dog {
    private $Name;
    protected function bark() {
      print "Woof!\n";
    }
} class poodle extends dog {
    // nothing happening here
} $mydog = new poodle; $mydog->Name = "Poppy"; var_dump($mydog); ?> Outputs this: object(poodle)#1 (2) { [""]=> NULL ["Name"]=> string(5) "Poppy" } I'm not sure what the [""]=>NULL is, but it isn't there if I shift $Name into the poodle class and make it public. --Paul -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php


« previous php.internals (#3389) next »