PHP高級對象構(gòu)建 多個構(gòu)造函數(shù)的使用
更新時間:2012年02月05日 23:49:30 作者:
構(gòu)建對象是PHP面向?qū)ο缶幊淘O(shè)計(jì)中的一個重要主題。在最簡單的情況下,普通構(gòu)造函數(shù)就夠用了,但如果要開展更為復(fù)雜的設(shè)計(jì),那么構(gòu)造函數(shù)可能會變的難以管理
下面就用一段代碼示例來演示一下PHP高級對象構(gòu)建中的使用多個構(gòu)造函數(shù)進(jìn)行對象構(gòu)建的原理。
<?php
class classUtil {//這是一個參數(shù)處理的類
public static function typeof($var){
if (is_object($var)) return get_class($var);//如果是對象,獲取類名
if (is_array($var)) return "array";//如果是數(shù)組,返回"array"
if (is_numeric($var)) return "numeric";//如果是數(shù)字,返回"numeric"
return "string";//字符串返回 "string"
}
public static function typelist($args){
return array_map(array("self","typeof"),$args);//數(shù)組循環(huán)通過調(diào)用self::typeof處理$args中的每個元素
}
public static function callMethodForArgs($object,$args,$name="construct"){
$method=$name."_".implode("_",self::typelist($args));//implode 是把數(shù)組元素用"_"連接成一個字符串
if (!is_callable(array($object,$method))){//is_callable()函數(shù)測試$object::$method是不是可調(diào)用的結(jié)構(gòu)
echo sprintf("Class %s has no methd '$name' that takes".
"arguments (%s)",get_class($object),implode(",",self::typelist($args)));
call_user_func_array(array($object,$method),$args);//call_user_func_array函數(shù)調(diào)用$object::$method($args)
}
}
}
class dateAndTime {
private $timetamp;
public function __construct(){//自身的構(gòu)造函數(shù)
$args=func_get_args();//獲取參數(shù)
classUtil::callMethodForArgs($this,$args);//調(diào)用參數(shù)處理類的方法
}
public function construct_(){//參數(shù)為空的時候
$this->timetamp=time();
}
public function construct_dateAndTime($datetime){//為類自身的時候
$this->timetamp=$datetime->getTimetamp();
}
public function construct_number($timestamp){//為數(shù)字的時候
$this->timetamp=$timestamp;
}
public function construct_string($string){//為時間型字符串時候
$this->timetamp=strtotime($string);
}
public function getTimetamp(){//獲取時間戳的方法
return $this->timetamp;
}
}
?>
以上方法,就說明了多個構(gòu)造函數(shù)的使用方法,其實(shí),很簡單,主要是對參數(shù)進(jìn)行了處理,不管是參數(shù)是字符,還是數(shù)字,還是類,都先進(jìn)了不同的處理,這樣就加大了代碼的靈活性。
復(fù)制代碼 代碼如下:
<?php
class classUtil {//這是一個參數(shù)處理的類
public static function typeof($var){
if (is_object($var)) return get_class($var);//如果是對象,獲取類名
if (is_array($var)) return "array";//如果是數(shù)組,返回"array"
if (is_numeric($var)) return "numeric";//如果是數(shù)字,返回"numeric"
return "string";//字符串返回 "string"
}
public static function typelist($args){
return array_map(array("self","typeof"),$args);//數(shù)組循環(huán)通過調(diào)用self::typeof處理$args中的每個元素
}
public static function callMethodForArgs($object,$args,$name="construct"){
$method=$name."_".implode("_",self::typelist($args));//implode 是把數(shù)組元素用"_"連接成一個字符串
if (!is_callable(array($object,$method))){//is_callable()函數(shù)測試$object::$method是不是可調(diào)用的結(jié)構(gòu)
echo sprintf("Class %s has no methd '$name' that takes".
"arguments (%s)",get_class($object),implode(",",self::typelist($args)));
call_user_func_array(array($object,$method),$args);//call_user_func_array函數(shù)調(diào)用$object::$method($args)
}
}
}
class dateAndTime {
private $timetamp;
public function __construct(){//自身的構(gòu)造函數(shù)
$args=func_get_args();//獲取參數(shù)
classUtil::callMethodForArgs($this,$args);//調(diào)用參數(shù)處理類的方法
}
public function construct_(){//參數(shù)為空的時候
$this->timetamp=time();
}
public function construct_dateAndTime($datetime){//為類自身的時候
$this->timetamp=$datetime->getTimetamp();
}
public function construct_number($timestamp){//為數(shù)字的時候
$this->timetamp=$timestamp;
}
public function construct_string($string){//為時間型字符串時候
$this->timetamp=strtotime($string);
}
public function getTimetamp(){//獲取時間戳的方法
return $this->timetamp;
}
}
?>
以上方法,就說明了多個構(gòu)造函數(shù)的使用方法,其實(shí),很簡單,主要是對參數(shù)進(jìn)行了處理,不管是參數(shù)是字符,還是數(shù)字,還是類,都先進(jìn)了不同的處理,這樣就加大了代碼的靈活性。
相關(guān)文章
php json_encode()函數(shù)返回json數(shù)據(jù)實(shí)例代碼
php返回json數(shù)據(jù)用到j(luò)son_encode()函數(shù),此函數(shù)會生成一個標(biāo)準(zhǔn)的json格式的數(shù)據(jù),實(shí)例代碼如下2014-10-10
yii2項(xiàng)目實(shí)戰(zhàn)之restful api授權(quán)驗(yàn)證詳解
這篇文章主要給大家介紹了關(guān)于yii2項(xiàng)目實(shí)戰(zhàn)之restful api授權(quán)驗(yàn)證的相關(guān)資料,文中介紹的非常詳細(xì),對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。2017-05-05
詳解PHP執(zhí)行定時任務(wù)的實(shí)現(xiàn)思路
這篇文章主要介紹了詳解PHP執(zhí)行定時任務(wù)的幾種實(shí)現(xiàn)思路,PHP的定時任務(wù)功能必須通過和其他工具結(jié)合才能實(shí)現(xiàn),們就來深入的解析幾種常見的php定時任務(wù)的思路2015-12-12
Notice: Undefined index: page in E:\PHP\test.php on line 14
Notice: Undefined index: page in E:\PHP\test.php on line 142010-11-11
Highcharts?圖表中圖例顯示狀態(tài)存儲的功能設(shè)計(jì)詳解
這篇文章主要介紹了Highcharts?圖表中圖例顯示狀態(tài)存儲的功能設(shè)計(jì)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
PHP+Mysql日期時間如何轉(zhuǎn)換(UNIX時間戳和格式化日期)
UNIX時間戳和格式化日期是我們常打交道的兩個時間表示形式,Unix時間戳存儲、處理方便,但是不直觀,格式化日期直觀,但是處理起來不如Unix時間戳那么自如,所以有的時候需要互相轉(zhuǎn)換,下面給出互相轉(zhuǎn)換的幾種轉(zhuǎn)換方式2012-07-07
php中convert_uuencode()與convert_uuencode函數(shù)用法實(shí)例
這篇文章主要介紹了php中convert_uuencode()與convert_uuencode函數(shù)用法,以實(shí)例形式了convert_uuencode()與convert_uuencode進(jìn)行編碼與解碼的方法,是非常實(shí)用的技巧,需要的朋友可以參考下2014-11-11

