note 59666 deleted from language.oop5.visibility by felipe
| From: | felipe@php.net | Date: | Sat, 15 Dec 2007 01:27:16 +0000 |
| Subject: | note 59666 deleted from language.oop5.visibility by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-133493@lists.php.net to get a copy of this message | ||
Note Submitter: jfk at 206-225-86-169 dot dedicated dot abac dot net
----
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!