__invoke,try{}catch(){},microtime(),is_callable()
生活随笔
收集整理的這篇文章主要介紹了
__invoke,try{}catch(){},microtime(),is_callable()
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
<?php
/*1.對象本身不能直接當(dāng)函數(shù)用,如果被當(dāng)做函數(shù)用,會直接回調(diào)__invoke方法* 2.驗證變量的內(nèi)容能否作為函數(shù)調(diào)用* 3.try{}catch(Exception $e){}catch(){}finally{}* 4.microtime()函數(shù)返回當(dāng)前時間戳和微妙數(shù)* */
class httpException extends Exception{}
class Testcallable{public function test(){echo '我在測試';echo '</br>';}public function __call($name,$arg){if($name =='othertest'){call_user_func_array([$this,'test'],$arg);}}public function __invoke() //對象本身不能直接當(dāng)函數(shù)用,如果被當(dāng)做函數(shù)用,會直接回調(diào)__invoke方法{echo '兄弟我是對象';echo '</br>';throw new Exception('掉錯了');}
}
$test = new Testcallable;
echo $test->test();
echo $test->othertest();//對象調(diào)用不存在的方法時,__call魔術(shù)方法會被調(diào)用
echo '-----------<br>';
echo is_callable([$test,'test']);//驗證變量的內(nèi)容能否作為函數(shù)調(diào)用
echo is_callable([$test,'othertest'],false,$call);
try{echo 44;/*$test();*/
}catch(httpException $e){echo 'htpp'.$e->getMessage();
}catch(Exception $e){echo $e->getMessage();
}finally{//程序又沒有錯誤都會執(zhí)行echo '失敗了';echo '-----------<br>';
}
echo $shijian = microtime();
轉(zhuǎn)載于:https://www.cnblogs.com/zxqblogrecord/p/10364480.html
總結(jié)
以上是生活随笔為你收集整理的__invoke,try{}catch(){},microtime(),is_callable()的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 循环链表实现两个多项式相加主要代码
- 下一篇: shell字符串的用法