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


--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



Reply via email to