Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:3383 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 89538 invoked from network); 12 Jul 2003 12:46:19 -0000 Received: from unknown (HELO alanathome.ossc.com.hk) (202.81.246.113) by pb1.pair.com with SMTP; 12 Jul 2003 12:46:19 -0000 Received: from devel ([192.168.0.40] helo=akbkhome.com ident=alan) by alanathome.ossc.com.hk with esmtp (Exim 4.20) id 19bJn4-00071a-Uc; Sat, 12 Jul 2003 20:47:50 +0800 Message-ID: <3F10035D.7040004@akbkhome.com> Date: Sat, 12 Jul 2003 20:47:25 +0800 User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4b) Gecko/20030520 X-Accept-Language: en-us, en MIME-Version: 1.0 To: hudzilla@php.net CC: internals@lists.php.net References: <200307121114.39525.hudzilla@php.net> In-Reply-To: <200307121114.39525.hudzilla@php.net> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Fwd: Re: [PHP] Private and protected variables in PHP 5? From: alan@akbkhome.com (Alan Knowles) I think what you are getting at is that you can set 'private' variable from outside.. - with no warnings etc. From the message on ZE2 when I played with this - and was supprised (although it does seem to make sense when you think about it) for all purposes, internal privates do not appear to exist to external code, (or even extended classes - although I've not tested this and would be interested to see if it does indeed work = and what it print_r's) 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..? Regards Alan Paul Hudson wrote: > All, > > I sent this originally to the php-general mailing list, but there doesn't > appear to be any consensus on how private variables should act. Any ideas? > > Thanks, > > > --Paul > > > ---------- Forwarded Message ---------- > > Subject: Re: [PHP] Private and protected variables in PHP 5? > Date: Saturday 12 Jul 2003 10:51 am > From: Paul Hudson > To: Greg Beaver , Alan D'Angelo > > > Greg, > > Meh - I thought namespaces were canned? Or is it just that the Poppy object > gets the private variables, but when I say $poppy->Name, PHP creates a new > public $Name variable and sets that? > > Anyway, I've moved this from php-general to php-internals in the hope that we > can get a sure answer along the lines of "This isn't a bug" > > Anyone? > > > --Paul > > On Saturday 12 Jul 2003 12:27 am, Greg Beaver wrote: > >>Hi to both of you, >> >>What is happening here is that there is a separate namespace for private >> elements in PHP 5. You can have both a private $Name and a public >>$Name if it is defined at runtime. I don't know if this is a feature or >>a bug, I'd call it a bug since redeclaration of a variable is not >>allowed, perhaps we should include the developers in on this question? >> >>Try this script to see the duplicate $Name variable: >> >>> class dog { >> // declare two private variables >> private $Name; >> private $DogTag; >> >> public function bark() { >> print "Woof!\n"; >> } >> >> public function printName() { >> print $this->Name; // prints nothing! >> } >> } >> >> // 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. "\n"; >> $poppy->printName(); >> print_r($poppy); >>?> >> >>outputs: >> >>Poppy >>poodle Object >>( >> [Name:private] => >> [DogTag:private] => >> [Name] => Poppy >>) >> >>Regards, >>Greg >>-- >>phpDocumentor >>http://www.phpdoc.org >> >>Alan D'Angelo wrote: >> >>>Hello, >>>In my PHP5 installation the first example print Poppy, >>>but the second return >>>Fatal error: Call to protected method dog::bark() from context '' in >>>c:\appserv\www\test\mailingphp50.php on line 18 >>> >>>In my previous installation oh PHP5, private variable worked well ... >>>PHP 5 is one beta, try with an next snapshot. >>> >>> >>>Alan >>> >>> >>>----- Original Message ----- >>>From: "Paul Hudson" >>>To: >>>Sent: Friday, July 11, 2003 11:21 PM >>>Subject: [PHP] Private and protected variables in PHP 5? >>> >>> >>>>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: >>>> >>>>>>> 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: >>>> >>>>>>> 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 >>>> >>>>-- >>>>PHP General Mailing List (http://www.php.net/) >>>>To unsubscribe, visit: http://www.php.net/unsub.php > > > ------------------------------------------------------- > > -- Can you help out? Need Consulting Services or Know of a Job? http://www.akbkhome.com