Created
June 8, 2013 12:24
-
-
Save HogeTatu/5735002 to your computer and use it in GitHub Desktop.
PHP ObjectBase
This file contains 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 | |
abstract class ObjectBase | |
{ | |
private $_fieldValueList = array(); | |
private $_fieldTypeList = array(); | |
private $_lock = false; | |
public final function __construct() | |
{ | |
$privateFieldList = self::_getPrivateFieldList(); | |
foreach ($this as $label => $value) { | |
if (isset($privateFieldList[$label])) continue; | |
$this->_fieldTypeList[$label] = gettype($value); | |
$this->_fieldValueList[$label] = $value; | |
unset($this->{$label}); | |
} | |
if (method_exists($this, '_initialize')) { | |
call_user_func_array(array($this, '_initialize'), func_get_args()); | |
} | |
} | |
private static final function _getPrivateFieldList() | |
{ | |
static $privateFieldList = null; | |
if ($privateFieldList !== null) return $privateFieldList; | |
$privateFieldList = array(); | |
$classVars = get_class_vars(get_class()); | |
foreach ($classVars as $label => $value) { | |
$privateFieldList[$label] = $label; | |
} | |
return $privateFieldList; | |
} | |
public final function __get($label) | |
{ | |
if (isset($this->_fieldTypeList[$label]) === false) { | |
throw new Exception("{$label} is not found in field list."); | |
} | |
return $this->_fieldValueList[$label]; | |
} | |
public final function __set($label, $value) | |
{ | |
if ($this->_lock) { | |
throw new Exception("field list is locked."); | |
} | |
if (isset($this->_fieldTypeList[$label]) === false) { | |
throw new Exception("{$label} is not found in field list."); | |
} | |
$oldType = $this->_fieldTypeList[$label]; | |
$newType = gettype($value); | |
if ($oldType !== 'NULL' && $oldType !== $newType) { | |
throw new Exception("type of {$label} is {$oldType} rather than {$newType}."); | |
} | |
$this->_fieldTypeList[$label] = $newType; | |
$this->_fieldValueList[$label] = $value; | |
} | |
public function lock() | |
{ | |
$this->_lock = true; | |
} | |
public function toArray() | |
{ | |
return $this->_fieldValueList; | |
} | |
} | |
//========================================================= | |
class Hoge extends ObjectBase | |
{ | |
public $foo = 1; | |
public $bar = null; | |
protected function _initialize($a, $b) | |
{ | |
echo "a = {$a}\n"; | |
echo "b = {$b}\n"; | |
} | |
} | |
//========================================================= | |
$hoge = new Hoge(1, 2); | |
$hoge->foo = 2; // OK | |
//$hoge->foo = 2.5; // NG | |
echo "{$hoge->foo}\n"; | |
$hoge->bar = 1.5; // OK | |
//$hoge->bar = 1; // NG | |
echo "{$hoge->bar}\n"; | |
$hoge->lock(); | |
//$hoge->foo = 3; // NG | |
var_dump($hoge->toArray()); | |
/* | |
a = 1 | |
b = 2 | |
2 | |
1.5 | |
array(2) { | |
["foo"]=> | |
int(2) | |
["bar"]=> | |
float(1.5) | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment