|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2003-11-19 17:49 UTC] 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?
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Nov 06 10:00:01 2025 UTC |
Excuse me, but you are *totally* missing my point here. You are telling me things i had already pointed out in the first post. I'll reduce my feature request to one simple line: Can *at least* a "notice" be triggered when a private member variable does not exist but is accessed? And by accessed i do NOT mean assigned! You're saying it behaves the same way php4 does, but that's just plain bs. See the following code in PHP4: <? error_reporting ( E_ALL ); class Test { function getMember () { return $this->member; } } $t = new Test (); echo $t->getMember (); ?> This would yield the following notice: Notice: Undefined property: member in test.php on line 5 All i'm asking is that some notice of the SAME sort can be implemented in php5 when trying to access parent private members.I've tested this with php5-200312222030 snapshot and I get two notices, when using this code (which is correct): <? error_reporting ( E_ALL ); class Test { private $member = "member"; } class DeriveTest extends Test { function __toString() { return "Member: {$this->member}, nonexistent {$this->nonexistent}"; } } $o = new DeriveTest (); echo $o, '<br>'; highlight_file(__FILE__); ?> It could, however, be done even better if it were a bit more explainatory notice. Like Java's error for instance: DeriveTest.java:5: member2 has private access in Test return "Member contains: " + member2 + " and getMember sais: " + nonexistent; ^ DeriveTest.java:5: cannot resolve symbol symbol : variable nonexistent location: class DeriveTest return "Member contains: " + member2 + " and getMember sais: " + nonexistent; ^ 2 errors I.e. a distinction between nonexistent members and private members.