Skip to content

Hashable::equals signature #140

Open
@githubeing

Description

@githubeing

Current Hashable::equals signature is equals($obj): bool, but in php docs it's said to be

equals ( object $obj ) : bool
https://www.php.net/manual/en/ds-hashable.equals.php

which differs. This difference leads to fatal for me:

Fatal error: Declaration of MyHashableImplementation::equals(object $obj): bool must be compatible with Ds\Hashable::equals($obj): bool in ...

First, I decided that this is a docs mistake, and was going to report it there. But then I understood that the docs is right in that the type declaration has to be there, because

It's guaranteed that obj is an instance of the same class.

and how do you suppose to implement this interface in scalars? Thus the object type is guaranteed.

But it's still possible to call

$otherHashable = null;
$hashable->equals($otherHashable);

Therefore, the signature has to be: equals(?object $obj)) i.e. nullable object to prevent null errors.

I propose you to add the type declaration to your code. Or at least, if you stick to current version, could you fix it in the docs?

And secondly, I'd appreciate if you explain the meaning of the phrase

It's guaranteed that obj is an instance of the same class.

Which code will guarantee this? I've just checked it:

$myHashable::equals($scalar)

doesn't produce any error or something...
Does the author mean that this is me who must must guarantee it? Do I have to write something like the following when implementing Hashable?

if(!$obj instanceof $this) throw new InvalidArgumentException

or is it supposed that ext-ds will somehow magically check this for me? If the latter, then I'd also like to file a bug report ('cause it doesn't check this)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions