1) { throw new exception(value must be 1 or below); } return true; } //在 try 代码块中触发异常 try { checknum(2); //if the exception is thrown, this text will not be shown echo 'if you see this, the number is 1 or below'; } //捕获异常 catch(**exception $e**) { echo 'message: ' .$e->getmessage(); } ?>
php是弱类型语言,catch中为什么要写exception $e ,而不是直接写$e
回复内容: 1) { throw new exception(value must be 1 or below); } return true; } //在 try 代码块中触发异常 try { checknum(2); //if the exception is thrown, this text will not be shown echo 'if you see this, the number is 1 or below'; } //捕获异常 catch(**exception $e**) { echo 'message: ' .$e->getmessage(); } ?>
php是弱类型语言,catch中为什么要写exception $e ,而不是直接写$e
异常本来就是以类型为基础的啊
php5开始, 可以对函数参数进行类型约束:http://php.net/manual/zh/language.oop5.typehinting.php
var; } /** * 另一个测试函数 * 第一个参数必须为数组 */ public function test_array(array $input_array) { print_r($input_array); }}//另外一个类class otherclass { public $var = 'hello world';}
类型约束只支持对象 和 数组(php 5.1之后)两种类型。而不支持整型 和 字符串类型。
对,有点像java