Skip to content

Instantly share code, notes, and snippets.

@ngyuki
Created October 22, 2012 02:44
Show Gist options
  • Save ngyuki/3929351 to your computer and use it in GitHub Desktop.
Save ngyuki/3929351 to your computer and use it in GitHub Desktop.
<?php
class Hoge
{
protected $_val;
public function __construct($val)
{
$this->_val = $val;
}
public function say()
{
echo "say $this->_val\n";
}
}
class HogeSub extends Hoge
{
public static function setVal(Hoge $hoge, $val)
{
$hoge->_val = $val;
}
}
class HogeProtect extends Hoge
{
private $_obj;
public function __construct($obj)
{
$this->_obj = $obj;
}
public function __get($name)
{
return $this->_obj->$name;
}
public function __set($name, $val)
{
$this->_obj->$name = $val;
}
}
///
$hoge = new Hoge(1);
// say 1
$hoge->say();
HogeSub::setVal($hoge, 2);
// say 2
$hoge->say();
$hogeProtect = new HogeProtect($hoge);
$hogeProtect->_val = 3;
// say 3
$hoge->say();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment