Skip to content

Instantly share code, notes, and snippets.

@GodsBoss
Created May 7, 2011 19:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save GodsBoss/960745 to your computer and use it in GitHub Desktop.
Save GodsBoss/960745 to your computer and use it in GitHub Desktop.
Access to protected base class members of a subclass via another subclass.
<?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!
@md2perpe
Copy link

Of course it works. Why wouldn't it? You're not going through GoodChild::setX().
Try https://gist.github.com/999471 instead.

@GodsBoss
Copy link
Author

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