<?php
function test_array(array $input_array){
  print_r($input_array);
}
function test_int(int $input_int){
  print_r($input_int);
}
test_array(array());//正确
test_int(1);//错误,这是何原因呢?
?>

解决方案 »

  1.   


    ///这样写,能达到要求
    function test_int($input_int){
      echo (int)$input_int;
    }
      

  2.   

    呃,这和java不一样吧..还真没研究过,int型的.
      

  3.   


    是不是说int型不能实现强制检查,array能?但上面的代码中,定义部分没错误啊,只是调用时,提示,参数必须为int类型,且不能空。
      

  4.   

    手册
    ============================================================
    Type Hinting
    PHP 5 introduces Type Hinting. Functions are now able to force parameters to be objects (by specifying the name of the class in the function prototype) or arrays (since PHP 5.1). Type Hints can only be of the object and array (since PHP 5.1) type. Traditional type hinting with int and string isn't supported. 暂时只支持object和array.