Skip to content

Instantly share code, notes, and snippets.

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