Skip to content

[7.2] Add polyfill for spl_object_id() #97

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 11, 2017
Merged

[7.2] Add polyfill for spl_object_id() #97

merged 1 commit into from
Aug 11, 2017

Conversation

nicolas-grekas
Copy link
Member

@nicolas-grekas nicolas-grekas commented Jul 11, 2017


public static function spl_object_id($object)
{
if (null === self::$hashMask) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use an early return here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure it would help, L108 and L111 are not the same, so that would require duplicating L111, is that what you mean?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually hashMask is calculated without usage of input param here$object. maybe extract it to separated function and then

public static function spl_object_id($object)
{
    return self::getHashMask() ^ hexdec(substr(spl_object_hash($object), self::$hashOffset, PHP_INT_SIZE));
}

?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer raw perf, thus prevent an extra call here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here we are, init logic split in separate function, called only once

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

public function testSplObjectId()
{
$obj = new \stdClass();
$id = spl_object_id($obj);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please test that calling spl_object_id for non-object works the same as native function

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@stof
Copy link
Member

stof commented Jul 11, 2017

@nicolas-grekas I suppose that VarDumper can then be migrated to use the new spl_object_id function, right ?

@stof
Copy link
Member

stof commented Jul 11, 2017

note: merging this PR must wait until the PR is merged in PHP itself. We don't want to merge the polyfill if they reject the PR.

private static function initHashMask()
{
$obj = (object) array();
self::$hashOffset = 16 - PHP_INT_SIZE;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you want to improve perf even further, you could even fully qualify these constants (as done for \PHP_VERSION_ID). Not sure it is worth it though (this code is called only once)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done by removing $hashOffset

@nicolas-grekas
Copy link
Member Author

@stof VarDumper can use this polyfill only in master, because of HHVM compat...

@robfrawley
Copy link
Contributor

The spl_object_id() function now merged upstream in php/php-src@5097e2e (also see php/php-src#2611 (comment)) so this can now be safely merged here, as well.

@fabpot
Copy link
Member

fabpot commented Aug 11, 2017

Thank you @nicolas-grekas.

@fabpot fabpot merged commit 06995f5 into symfony:master Aug 11, 2017
fabpot added a commit that referenced this pull request Aug 11, 2017
This PR was merged into the 1.5-dev branch.

Discussion
----------

[7.2] Add polyfill for spl_object_id()

As [discussed on php-internals](https://externals.io/message/99751) and implemented in php/php-src#2611.
Implementation [borrowed from VarDumper](https://github.com/symfony/symfony/blob/2.7/src/Symfony/Component/VarDumper/Cloner/VarCloner.php#L300).

Commits
-------

06995f5 [7.2] Add polyfill for spl_object_id()
@robfrawley
Copy link
Contributor

@fabpot Sorry to comment on a closed PR, but I don't think this is worth a new issue: it would be great to get a release tagged that includes this PR (I hate having to include dev-master in my composer files). :-)

@fabpot
Copy link
Member

fabpot commented Aug 16, 2017

@robfrawley Done, 1.5 is out now.

@nicolas-grekas nicolas-grekas deleted the spl-obj-id branch October 11, 2017 08:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants