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 <[EMAIL PROTECTED]>
To: Greg Beaver <[EMAIL PROTECTED]>, Alan D'Angelo <[EMAIL PROTECTED]>


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:

<?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:

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" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> 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


-------------------------------------------------------




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


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



Reply via email to