Welcome! Log In Create A New Profile

Advanced

[PHP-DEV] Add \Reflection*::getHash()

Posted by Andreas Hennings 
Andreas Hennings
[PHP-DEV] Add \Reflection*::getHash()
December 11, 2017 03:20AM
I propose to add a new method ::getHash() to reflection API classes.

Such that:

Two reflectors return the same hash, if and only if:

1. the reflected items have the same type (class, method, parameter etc), AND.
2. the reflected items have the same names, AND.
3. the reflected items have the same PHP code (e.g. for distinct
processes), AND.
4. optionally: Every base class, every implemented interface and every
used trait has the same PHP code (and thus, the same hash).
5. 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.

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
Andreas Hennings
[PHP-DEV] Re: Add \Reflection*::getHash()
December 11, 2017 04:10AM
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.


On 11 December 2017 at 03:11, Andreas Hennings <[email protected]> wrote:
> I propose to add a new method ::getHash() to reflection API classes.
>
> Such that:
>
> Two reflectors return the same hash, if and only if:
>
> 1. the reflected items have the same type (class, method, parameter etc), AND.
> 2. the reflected items have the same names, AND.
> 3. the reflected items have the same PHP code (e.g. for distinct
> processes), AND.
> 4. optionally: Every base class, every implemented interface and every
> used trait has the same PHP code (and thus, the same hash).
> 5. 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.

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
Andreas Hennings
[PHP-DEV] Re: Add \Reflection*::getHash()
December 11, 2017 04:10AM
On 11 December 2017 at 04:03, Andreas Hennings <[email protected]> wrote:
> 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.

If the method is instead named ::getDeclarationHash(), it will be
clear that ReflectionObject->getDeclarationHash() will be the same as
for the instantiated class.

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
Marco Pivetta
Re: [PHP-DEV] Re: Add \Reflection*::getHash()
December 11, 2017 09:10AM
On 11 Dec 2017 04:04, "Andreas Hennings" <[email protected]> wrote:

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.


The reflected object is not GC'd until the `ReflectionObject` instance is
GC'd, so the API would be redundant. Maybe just produce the same hash as
`ReflectionClass`, since those are often used interchangeably?
Sorry, only registered users may post in this forum.

Click here to login