__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();

  

原文地址:https://www.cnblogs.com/zxqblogrecord/p/10364480.html