Search code examples
phpmigrationcompatibilityphp-7.2

Move ext/hash from resources to objects php PHP 7.2 migration


As part of the long-term migration away from resources, the Hash extension has been updated to use objects instead of resources. The change should be seamless for PHP developers, except for where is_resource() checks have been made (which will need updating to is_object() instead).

The "Hash extension has been update to use objects instead of resources I do not understand

1) Where in the "hash extension" is there this change represented?.

2) the second sentences "which will need update", refers, I understand to is_resource in a Hash function (extension part), but, how this is the case exactly? Which will be an exact example that "should be replaced" ?

Thanks.


Solution

  • Prior to 7.2 a method like...

    <?php
    $ctx = hash_init('md5');
    var_dump($ctx);
    

    ...would output something like resource(7, Hash Context). The returned value was a "resource".

    A resource is a special variable, holding a reference to an external resource.

    As of 7.2 they changed that to return an actual object (class) of type HashContext.

    You would therefor possibly need to change how the return value of those hash functions are being evaluated.

    Where before you might simply check is_resource() (possibly get_resource_type()) now you would use is_a or instanceof.

    if ($h instanceof \HashContext) { ...
    

    Really depends on the code. You might not need to change anything. Depends if/how/where your code evaluates the return values of some of those functions. Stuff you might find in a unit test...

    For example, this code would still work pre/post 7.2 even though $ctx changes type:

    $ctx = hash_init('md5');
    hash_update($ctx, 'The quick brown fox ');
    hash_update($ctx, 'jumped over the lazy dog.');
    echo hash_final($ctx);