Created
May 7, 2011 19:06
-
-
Save GodsBoss/960745 to your computer and use it in GitHub Desktop.
Access to protected base class members of a subclass via another subclass.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Base{ | |
protected $x; | |
public function getX(){ | |
return $this->x;}} | |
/** | |
* This class tries to ensure that $x is always an integer. | |
*/ | |
class GoodChild extends Base{ | |
public function setX($x){ | |
if (is_integer($x)){ | |
$this->x=$x;}}} | |
/** | |
* This class circumvents GoodChild's security system. | |
*/ | |
class EvilChild extends Base{ | |
public function setX(Base $b, $x){ | |
$b->x=$x;}} | |
$good=new GoodChild(); | |
$evil=new EvilChild(); | |
$evil->setX($good, 'foo'); // Works! |
I see you got the point of this gist. :-) It's a case against 'protected'.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Of course it works. Why wouldn't it? You're not going through GoodChild::setX().
Try https://gist.github.com/999471 instead.