Asked  7 Months ago    Answers:  5   Viewed   36 times

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

For example:

    declare (strict_types=1);

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

    function test (resource $ch)


The above will give the error:

Fatal error: Uncaught TypeError: Argument 1 passed to test() must be an instance of resource, resource given

A var_dump on $ch reveals it to be resource(4, curl), and the manual says curl_init () returns a resource.

Is it at all possible to strictly type the test() function to support the $ch variable?



PHP does not have a type hint for resources because

No type hint for resources is added, as this would prevent moving from resources to objects for existing extensions, which some have already done (e.g. GMP).

However, you can use is_resource() within the function/method body to verify the passed argument and handle it as needed. A reusable version would be an assertion like this:

function assert_resource($resource)
    if (false === is_resource($resource)) {
        throw new InvalidArgumentException(
                'Argument must be a valid resource type. %s given.',

which you could then use within your code like that:

function test($ch)
    // do something with resource
Wednesday, March 31, 2021
answered 7 Months ago

JSON isn't a native datatype in PHP, it's a structured string. So if your function returns JSON, you're returning a string.

So function myFunction ($a) : string { } would be correct.

If you want to describe the return further you should be using docs.

 * @return string $jsonString The returned string contains JSON
function myFunction ($a) : string  { }

The same also goes for serialized objects in PHP. A serialized object is a structured string.

Wednesday, March 31, 2021
answered 7 Months ago

Though the splat operator ... is similar to call_user_func_array():

                     explode('@', $this->routes["authControllers"][$this->routes["uri"][$uri]]));

I think it would make more sense to pass the required arguments:

list($controller, $action) = explode('@', $this->routes["authControllers"][$this->routes["uri"][$uri]]);
$this->callAction($controller, $action);
Saturday, May 29, 2021
answered 5 Months ago

Try with actual PHPDoc for your getById() where you specify dynamic type (e.g. @return static or @return $this).

That's the most common way of providing such "this class and its' children" typehint.

Another possible option is kind of the same .. but using PHP 7.1 functionality (so no PHPDoc blocks).

Try using self as return type instead of Model. I mean here:

public static function getById(string $id) : ?Model {

use this instead:

public static function getById(string $id) : ?self {

But for this one you should use PhpStorm 2017.2 EAP build as 2017.1.x has issues with that (see as an example).

Saturday, May 29, 2021
answered 5 Months ago

StrictMode is turned on by default in Honeycomb.

See say link specifically penaltyDeathOnNetwork(). I ran into a similar problem.

Thursday, July 29, 2021
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :