實現(xiàn)了一個PHP5的getter/setter基類的代碼
更新時間:2007年02月25日 00:00:00 作者:
PHP3、PHP4都擁有類,但它們的類定義的實在很不像樣,效率還挺難為情的,但資料上說PHP5重新構(gòu)造了面向?qū)ο蟮闹С?,盡管并不是完全面向?qū)ο螅菜隳苣贸鰜硪娙肆恕?
昨天晚上閑著無聊便弄起這玩意,感覺PHP5增加的類成員權(quán)限關(guān)鍵字挺好,但問題又來了,似乎還沒一種方便的方式可以定義字段的getter以及setter,傳統(tǒng)的方式是這樣定義的:
class a
{
private $field;
public function get_field() { return $this->$field; }
public function set_field($value) { $this->field = $value; }
}
雖然實現(xiàn)起來挺容易,但是說實在的,為一個字段去寫這一堆代碼還真不爽。。
于是便思索著是不是有一種更方便的方式來解決,并且可以方便地定義它的類型限制什么的。
搗鼓了半天(沒辦法,對它不熟。。),終于弄出一個類來解決這個問題:
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);
這里邊實現(xiàn)了getter以及setter,但由于時間關(guān)系我沒去實現(xiàn)readonly的功能,其實很簡單,就是再加一項,標識它能不能被改寫就成
昨天晚上閑著無聊便弄起這玩意,感覺PHP5增加的類成員權(quán)限關(guān)鍵字挺好,但問題又來了,似乎還沒一種方便的方式可以定義字段的getter以及setter,傳統(tǒng)的方式是這樣定義的:
class a
{
private $field;
public function get_field() { return $this->$field; }
public function set_field($value) { $this->field = $value; }
}
雖然實現(xiàn)起來挺容易,但是說實在的,為一個字段去寫這一堆代碼還真不爽。。
于是便思索著是不是有一種更方便的方式來解決,并且可以方便地定義它的類型限制什么的。
搗鼓了半天(沒辦法,對它不熟。。),終于弄出一個類來解決這個問題:
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);
這里邊實現(xiàn)了getter以及setter,但由于時間關(guān)系我沒去實現(xiàn)readonly的功能,其實很簡單,就是再加一項,標識它能不能被改寫就成
您可能感興趣的文章:
- 關(guān)于__defineGetter__ 和__defineSetter__的說明
- JavaScript之Getters和Setters 平臺支持等詳細介紹
- javascript中的__defineGetter__和__defineSetter__介紹
- ECMAScript5中的對象存取器屬性:getter和setter介紹
- JavaScript中setter和getter方法介紹
- jQuery 3.0 的 setter和getter 模式詳解
- 使用node+vue.js實現(xiàn)SPA應用
- 基于Vue.js的表格分頁組件
- Vue.js每天必學之組件與組件間的通信
- 談談因Vue.js引發(fā)關(guān)于getter和setter的思考
相關(guān)文章
PHP MVC模式在網(wǎng)站架構(gòu)中的實現(xiàn)分析
MVC模式在網(wǎng)站架構(gòu)中十分常見。它允許我們建立一個三層結(jié)構(gòu)的應用程式,從代碼中分離出有用的層,幫助設(shè)計師和開發(fā)者協(xié)同工作以及提高我們維護和擴展既有程式的能力。2010-03-03理解PHP5中static和const關(guān)鍵字的區(qū)別
理解PHP5中static和const關(guān)鍵字的區(qū)別...2007-03-03利用swoole+redis實現(xiàn)股票和區(qū)塊鏈服務
這篇文章主要給大家介紹了關(guān)于利用swoole+redis實現(xiàn)股票和區(qū)塊鏈服務的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起學習學習下吧。2017-09-09