# Questionable private $varname behavior

- Thread: https://externals.io/message/3370
- Original archive: https://news-web.php.net/php.internals/3370
- Started by Greg Beaver on 2003-07-11T23:27:14+00:00
- Messages: 2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #3370 — Greg Beaver — 2003-07-11T23:27:14+00:00

URL: https://externals.io/message/3370#3370

Hi, a discussion on php.general led to the discovery of this oddly legal 
script:

<?php
   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
)

Is this expected behavior or should I open a bug?

Greg

---

## #3388 — Andi Gutmans — 2003-07-12T17:16:51+00:00

URL: https://externals.io/message/3370#3388 · In reply to: #3370

At 07:27 PM 11/7/2003 -0400, Greg Beaver wrote:
>Hi, a discussion on php.general led to the discovery of this oddly legal 
>script:
>
><?php
>   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
>)
>
>Is this expected behavior or should I open a bug?

Seems to be expected behavior to me. poodle doesn't *know* the private 
variable "Name" so when you use $poppy->Name it declares a public member 
instead. As printName() is inherited from the base class it accesses the 
private member and not the public one.

Andi
