天天看點

__invoke,try{}catch(){},microtime(),is_callable()

<?php
/*1.對象本身不能直接當函數用,如果被當做函數用,會直接回調__invoke方法
 * 2.驗證變量的内容能否作為函數調用
 * 3.try{}catch(Exception $e){}catch(){}finally{}
 * 4.microtime()函數傳回目前時間戳和微妙數
 * */
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() //對象本身不能直接當函數用,如果被當做函數用,會直接回調__invoke方法
    {
        echo '兄弟我是對象';
        echo '</br>';
        throw new Exception('掉錯了');
    }
}
$test = new Testcallable;
echo $test->test();
echo $test->othertest();//對象調用不存在的方法時,__call魔術方法會被調用
echo '-----------<br>';
echo is_callable([$test,'test']);//驗證變量的内容能否作為函數調用
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{
    //程式又沒有錯誤都會執行
    echo '失敗了';
    echo '-----------<br>';
}
echo $shijian = microtime();      

繼續閱讀