PHP 7 and strict “resource” types

后端 未结 2 1053
北恋
北恋 2020-12-11 00:01

Does PHP 7 support strict typing for resources? If so, how?

For example:

    declare (strict_types=1);

    $ch = curl_init ();
    test ($ch);

             


        
2条回答
  •  独厮守ぢ
    2020-12-11 00:57

    resource is not a valid type so it's assumed to be a class name as per good old PHP/5 type hints. But curl_init() does not return an object instance.

    As far as I know there's not way to specify a resource. It probably wouldn't be so useful since not all resources are identical: a resource generated by fopen() would be useless for oci_parse().

    P.S. If you want to check the resource in the function body, you can use get_resource_type() (with is_resource() to prevent errors), as in:

    is_resource($ch) && get_resource_type($ch) === 'curl'
    

提交回复
热议问题