note 59666 added to language.oop5.visibility
| From: | jfk at 206-225-86-169 dot dedicated dot abac dot net | Date: | Tue, 13 Dec 2005 01:14:12 +0000 |
| Subject: | note 59666 added to language.oop5.visibility | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-100037@lists.php.net to get a copy of this message | ||
BEWARE!! The visibility of a class member can easily be compromised by attaining a reference to the
protected member. In C++ for example this can not be done unless specifically included in the
declaration/definition. However in PHP it is simple!!!! Observe....
class MyClass{
protected $num;
public function MyClass(){
$this->num = 0;
}
public function Increment(){
$this->num++;
}
public function GetNum(){
return $this->num;
}
};
$c = new MyClass(); //create instance of class
$x = &$c->GetNum(); //create variable referencing protected member to keep track of value
while avoiding repeat calls to GetNum().
$x++;
echo "CLASS VALUE: " . $c->GetNum() . " VAR VALUE: $x\n";
The output is:
CLASS VALUE: 1 VAR VALUE: 1
A protected member should be exactly what "protected" means, protected.
I hope I am overlooking something. If not I believe this feature should be corrected to improve
transparency in syntax between the two "languages."
Additionally, please note that my email address is:
jkallen@206-225-86-169.dedicated.abac.net
I had to create the jfk alias because the form for submitting notes does not accept emails as long
as mine!!!! Ouch!
----
Manual Page -- http://www.php.net/manual/en/language.oop5.visibility.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+59666
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+59666&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+59666&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+59666&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+59666&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+59666&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+59666&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+59666&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+59666&report=yes
Search -- http://master.php.net/manage/user-notes.php