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

- Thread: https://externals.io/message/3380
- Original archive: https://news-web.php.net/php.internals/3380
- Started by Paul Hudson on 2003-07-12T10:14:39+00:00
- Messages: 3
- 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

---

## #3380 — Paul Hudson — 2003-07-12T10:14:39+00:00

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

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 <hudzilla@php.net>
To: Greg Beaver <greg@chiaraquartet.net>, Alan D'Angelo <info@php-power.it>


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


> 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:
>
> <?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
> )
>
> Regards,
> Greg
> --
> phpDocumentor
> http://www.phpdoc.org
>
> Alan D'Angelo wrote:
> > […]

---

## #3383 — Alan Knowles — 2003-07-12T12:47:25+00:00

URL: https://externals.io/message/3380#3383 · In reply to: #3380

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 <hudzilla@php.net>
> To: Greg Beaver <greg@chiaraquartet.net>, Alan D'Angelo <info@php-power.it>
> 
> 
> 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
> 
> 
> 
> > […]
> 
> 
> -------------------------------------------------------
> 
> 


-- 
Can you help out?
Need Consulting Services or Know of a Job?
http://www.akbkhome.com

---

## #3387 — Paul Hudson — 2003-07-12T16:10:10+00:00

URL: https://externals.io/message/3380#3387 · In reply to: #3383

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
