I propose to add a new method ::getHash() to reflection API classes.
Such that:
Two reflectors return the same hash, if and only if:
- the reflected items have the same type (class, method, parameter etc), AND.
- the reflected items have the same names, AND.
- the reflected items have the same PHP code (e.g. for distinct
processes), AND. - optionally: Every base class, every implemented interface and every
used trait has the same PHP code (and thus, the same hash). - optionally: The PHP version and PHP settings are the same. (*)
Such an id can be used as a cache key between requests.
It would automatically change / become invalid, if:
- new code changes are deployed, OR.
- different processes include different files that each declare the
same class, OR. - php is upgraded, or php settings change. (*)
Currently the best way (for practicality and performance) to achieve
something like this is to get the timestamp of the file that defines
the class.
Note: The behavior of a function can change if the code of another
function that is being called is modified.
The hash would still be the same, because it does not look at
functions being called.
(*) Maybe for the php version and settings there should instead be a
global hash, which can be used as salt for a combined cache key.
I was first going to propose to add this new method directly on
\Reflector interface.
But this would be a BC break, because userland classes that implement
this interface would break.
So instead, either
- add it to each class separately, or
- add a new interface \Reflector2 extends \Reflector (yeah, versioned
interfaces!), and let all reflection classes implement that.
So far this proposal only discusses reflections of PHP declarations / symbols.
What would we do for e.g. ReflectionObject?
Return the spl_object_hash()
?
Or return the hash of the instantiated class?
Or a hash of the serialized object?
We need to return something, because ReflectionObject extends ReflectionClass.
I propose to add a new method ::getHash() to reflection API classes.
Such that:
Two reflectors return the same hash, if and only if:
- the reflected items have the same type (class, method, parameter etc), AND.
- the reflected items have the same names, AND.
- the reflected items have the same PHP code (e.g. for distinct
processes), AND.- optionally: Every base class, every implemented interface and every
used trait has the same PHP code (and thus, the same hash).- optionally: The PHP version and PHP settings are the same. (*)
Such an id can be used as a cache key between requests.
It would automatically change / become invalid, if:
- new code changes are deployed, OR.
- different processes include different files that each declare the
same class, OR.- php is upgraded, or php settings change. (*)
Currently the best way (for practicality and performance) to achieve
something like this is to get the timestamp of the file that defines
the class.Note: The behavior of a function can change if the code of another
function that is being called is modified.
The hash would still be the same, because it does not look at
functions being called.(*) Maybe for the php version and settings there should instead be a
global hash, which can be used as salt for a combined cache key.I was first going to propose to add this new method directly on
\Reflector interface.
But this would be a BC break, because userland classes that implement
this interface would break.So instead, either
- add it to each class separately, or
- add a new interface \Reflector2 extends \Reflector (yeah, versioned
interfaces!), and let all reflection classes implement that.
So far this proposal only discusses reflections of PHP declarations / symbols.
What would we do for e.g. ReflectionObject?
Return thespl_object_hash()
?
Or return the hash of the instantiated class?
Or a hash of the serialized object?
We need to return something, because ReflectionObject extends ReflectionClass.
If the method is instead named ::getDeclarationHash(), it will be
clear that ReflectionObject->getDeclarationHash() will be the same as
for the instantiated class.