Table of Contents

PHP RFC: Migration Hash Context from Resource to Object

Introduction

Since PHP5, objects have been the preferred structure for wrapping internal data, however some clod created the hash extension to use resources. This RFC seeks to rectify that error by migrating the Hash extension to use an object implementation for hash contexts instead of a resource.

Proposal

Convert the opaque resource to an opaque object. This is the lightest touch change to ensure that existing code should continue to function unless it has explicit is_resource() checks. These checks can be easily replaced with is_resource|is_object checks instead.

Future Scope

Backward Incompatible Changes

Any userspace code which explicitly type checks HashContext values using is_resource()/get_resource_type()

Proposed PHP Version(s)

PHP 7.next

Open Issues

Finalizing a hash context currenly invalidates the resource via `zend_list_close()` making all future operations on that value fail. This patch emulates this by clearing the internal value `context` and verifying it in all function invocations using the value. This isn't typical object behavior, but minimizes BC breakage. We could potentially invoke hash_copy() to save the context prior to finalization, finalize it, then revert to the prior state by restoring the saved context. This makes sense to me, but introduces a change to behavior which needs to be discussed.

Proposed Voting Choices

Include one or both patches at https://github.com/php/php-src/pull/2309 50%+1 required.

This primary vote is to determine, overall, if the “hash context” resource used by hash_init/hash_update/hash_final should be changed to an object.

Change HashContext to an object
Real name Yes No
ashnazg (ashnazg)  
auroraeosrose (auroraeosrose)  
bishop (bishop)  
bwoebi (bwoebi)  
guilhermeblanco (guilhermeblanco)  
leigh (leigh)  
marcio (marcio)  
mariano (mariano)  
mike (mike)  
nikic (nikic)  
ocramius (ocramius)  
pollita (pollita)  
sammyk (sammyk)  
trowski (trowski)  
zimt (zimt)  
Final result: 15 0
This poll has been closed.

This second vote, contingent on passing the first vote, determines which behavior the new HashContext will adopt (per the “Open Issue” above).

Merge one or both diffs
Real name Frozen Reentrant
ashnazg (ashnazg)  
auroraeosrose (auroraeosrose)  
bishop (bishop)  
bwoebi (bwoebi)  
derick (derick)  
guilhermeblanco (guilhermeblanco)  
krakjoe (krakjoe)  
leigh (leigh)  
marcio (marcio)  
mariano (mariano)  
mike (mike)  
nikic (nikic)  
ocramius (ocramius)  
pollita (pollita)  
trowski (trowski)  
Final result: 9 6
This poll has been closed.

Voting opened: 2017-01-17 22:50:00 UTC
Voting closes: 2017-01-31 23:59:59 UTC

Patches and Tests