-
-
Save md2perpe/999471 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 | |
{ | |
private $x; | |
public function getX() | |
{ | |
return $this->x; | |
} | |
public function setX($x) | |
{ | |
$this->x = $x; | |
} | |
} | |
/** | |
* This class tries to ensure that $x is always an integer. | |
*/ | |
class GoodChild extends Base | |
{ | |
public function setX($x) | |
{ | |
if (is_integer($x)) | |
{ | |
parent::setX(x); | |
} | |
} | |
} | |
/** | |
* This class circumvents GoodChild's security system. | |
*/ | |
class EvilChild extends Base | |
{ | |
public function setX(Base $b, $x) | |
{ | |
$b->setX($x); | |
} | |
} | |
$good = new GoodChild(); | |
$evil = new EvilChild(); | |
$evil->setX($good, 'foo'); // Works not! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment