ID: 26325 Updated by: [EMAIL PROTECTED] Reported By: drm at melp dot nl -Status: Open +Status: Bogus Bug Type: Feature/Change Request Operating System: Windows XP PHP Version: 5.0.0b2 (beta2) New Comment:
There are two issues here. First, PHP 5 is just acting like PHP 4 did -- you can initialize a member of an object without explicitly declaring it in the class definition and PHP won't complain. Second, Test::$member and DeriveTest::$member aren't the same things. Test::$member being private, it's only accessible and visible from within Test. DeriveTest has no idea it exists, so it creates its own public member called $member. If you do a print_r($o) at the end of your script, you'll see that there are two members called $member, one of which is private and the other public. So yes, this is intended behaviour. One of the main ideas of private members is that they aren't even visible from derived classes. If you try to access a private member from a parent class, the derived class won't see it and will instead try using its own member, and if that doesn't exist it will implicitly create it. J Previous Comments: ------------------------------------------------------------------------ [2003-11-20 04:45:48] drm at melp dot nl Here's a more explanatory piece of code: http://gerard.yoursite.nl/php.net/private-members.phps ------------------------------------------------------------------------ [2003-11-19 17:49:24] drm at melp dot nl Description: ------------ Hi :) I read in the new features list of Zend Engine 2 that access modifiers like private/protected are introduced, but something really weird comes in mind when reading carefully. Private members are returned _empty_ wheing tried to be accessed from derived classes. "Intended behaviour", is said. OK, i can live with that. But even when i turned on notices with error_reporting, nothing is noticed? The case grows when looking at the following code sample. This could produce very weird bugs when writing PHP code (i hope you can see that ;)), so I would like to convince you all to at least implement a Notice when trying to access (non-defined) private (parent) members? It would do the coders not using E_ALL no harm :) Please consider this :) Reproduce code: --------------- error_reporting ( E_ALL ); class Test { private $member; function __construct () { $this->member = "Test constructor"; } function __toString () { return "Member contains: {$this->member}"; } function getMember () { return $this->member; } function setMember ( $m ) { $this->member = $m; } } class DeriveTest extends Test { function __construct () { parent::__construct (); } function __toString () { return "Member contains: {$this->member}, though getMember() says: " . $this->getMember() ."?"; } function setMember ( $m ) { $this->member = $m; } } $o = new DeriveTest (); echo $o, '<br>'; $o->setMember ( "a" ); echo $o, '<br>'; Expected result: ---------------- The expected result would be for me: Notice: undefined member ``DeriveTest::$member'' in ...\test.php on line 12 Member contains: , though getMember() says: Test constructor? Member contains: a, though getMember() says: Test constructor? or something of the sort Actual result: -------------- The actual result is: Member contains: , though getMember() says: Test constructor? Member contains: a, though getMember() says: Test constructor? ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=26325&edit=1