PHP3、PHP4都擁有類,但它們的類定義的實在很不像樣,效率還挺難為情的,但資料上說PHP5重新構造了面向對象的支持,盡管並不是完全面向對象,但也算能拿出來見人了。
昨天晚上閒著無聊便弄起這玩意,感覺PHP5增加的類成員權限關鍵字挺好,但問題又來了,似乎還沒一種方便的方式可以定義字段的getter以及setter,傳統的方式是這樣定義的:
class a
{
private $field;
public function get_field() { return $this->$field; }
public function set_field($value) { $this->field = $value; }
}
雖然實現起來挺容易,但是說實在的,為一個字段去寫這一堆代碼還真不爽。。
於是便思索著是不是有一種更方便的方式來解決,並且可以方便地定義它的類型限制什麼的。
搗鼓了半天(沒辦法,對它不熟。。),終於弄出一個類來解決這個問題:
class abstract_entity
{
private $fields;
private $sys_type = array(
"bool" => "",
"array" => "",
"double" => "",
"float" => "",
"int" => "",
"integer" => "",
"long " => "",
"null" => "",
"object" => "",
"real" => "",
"resource" => "",
"string" => ""
// "mixed" and "number"
);
protected function __construct($fields)
{
/*********************************\
* $fields = array(
* "id" = array(
* "allow_null" = false,
* "value" = 1,
* "type" = "int"
* );
* );
\**********************************/
$this->fields = $fields;
}
public function __get($key)
{
if(array_key_exists($key, $this->fields))
{
return $this->fields[$key]["value"];
}
else
{
throw new Exception("該屬性不存在");
}
}
public function __set($key, $value)
{
if(array_key_exists($key, $this->fields))
{
$allow_null = $this->fields[$key]["allow_null"];
$type = $this->fields[$key]["type"];
if(array_key_exists($type, $this->sys_type))
{
$fun = create_function('$value', "return is_$type($value);");
if(@$fun($value))
{
$this->fields[$key]["value"] = $value;
}
else if($allow_null && is_null($value))
{
$this->fields[$key]["value"] = NULL;
}
else
{
throw new Exception("該值類型不正確,必須為" . $type . "類型");
}
}
else if($type == "mixed")
{
if(!is_null($value))
{
$this->fields[$key]["value"] = $value;
}
else if($allow_null)
{
$this->fields[$key]["value"] = NULL;
}
else
{
throw new Exception("該值不允許為NULL值");
}
}
else if($type == "number")
{
if(is_int($value) || is_float($value))
{
$this->fields[$key]["value"] = $value;
}
else if(is_null($value) && $allow_null)
{
$this->fields[$key]["value"] = NULL;
}
else
{
throw new Exception("該值類型不正確,必須為" . $type . "類型");
}
}
else
{
if(class_exists($type) || interface_exists($type))
{
if(is_subclass_of($value, $type))
{
$this->fields[$key]["value"] = $value;
}
else if(is_null($value) && $allow_null)
{
$this->fields[$key]["value"] = NULL;
}
else
{
throw new Exception("該值類型不正確,必須為" . $type . "類型");
}
}
else if(is_null($value) && $allow_null)
{
$this->fields[$key]["value"] = NULL;
}
}
}
else
{
throw new Exception("該屬性不存在");
}
}
}
通過定義一個一定格式的array可以比較方便地定義該字段的類型、是否允許NULL值以及默認值。
測試代碼如下:
class test extends abstract_entity
{
public function __construct()
{
$define = array(
"id" => array(
"allow_null" => false,
"value" => 1,
"type" => "int"
),
"name" => array(
"allow_null" => false,
"value" => "abc",
"type" => "string"
),
"gender" => array(
"allow_null" => false,
"value" => true,
"type" => "bool"
),
"ins" => array(
"allow_null" => false,
"value" => $this,
"type" => "test"
),
"ins1" => array(
"allow_null" => true,
"value" => $this,
"type" => "test"
),
"ins2" => array(
"allow_null" => true,
"value" => NULL,
"type" => "config_media_type"
)
);
parent::__construct($define);
}
}
$a = new test();
$a->id = 123;
eche $a->id;
echo $a->ins1;
$a->ins1 = NULL;
echo is_null($a->ins1);
這裡邊實現了getter以及setter,但由於時間關系我沒去實現readonly的功能,其實很簡單,就是再加一項,標識它能不能被改寫就成