Re: Private and protected variables in PHP 5?
| From: | Alan D'Angelo | Date: | Fri, 11 Jul 2003 22:19:35 +0000 |
| Subject: | Re: Private and protected variables in PHP 5? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-154936@lists.php.net to get a copy of this message | ||
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" <hudzilla@php.net>
To: <php-general@lists.php.net>
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:
>
> <?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
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>