Skip to content

is_a() works with interfaces, too#506

Merged
Girgias merged 3 commits intophp:masterfrom
BenMorel:patch-10
Dec 22, 2022
Merged

is_a() works with interfaces, too#506
Girgias merged 3 commits intophp:masterfrom
BenMorel:patch-10

Conversation

@BenMorel
Copy link
Copy Markdown
Contributor

No description provided.

<refnamediv>
<refname>is_a</refname>
<refpurpose>Checks if the object is of this class or has this class as one of its parents</refpurpose>
<refpurpose>Checks if the object is of this class or has this class as one of its parents, or implements this interface</refpurpose>
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think the refpurpose is already too long; we should somehow shorten it. The additional mention of interfaces should go into the description section (first paragraph after the methodsynopsis), though.

<refnamediv>
<refname>is_a</refname>
<refpurpose>Checks if the object is of this class or has this class as one of its parents</refpurpose>
<refpurpose>Checks if the object is of this class or has this class as one of its parents, or implements this interface</refpurpose>
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

How about?

Suggested change
<refpurpose>Checks if the object is of this class or has this class as one of its parents, or implements this interface</refpurpose>
<refpurpose>Checks if the object is an instance of a class/interface</refpurpose>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I thought about that as well, but the first parameter may be a class/interface name.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
<refpurpose>Checks if the object is of this class or has this class as one of its parents, or implements this interface</refpurpose>
<refpurpose>Checks if the object is a subtype of a class/interface</refpurpose>

Maybe?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think "subtype" is worse than "instance of", since the former doesn't cover the case where the object is of that type. If in doubt, we should stick with "object" (for now), and change to "instance of". The description section could still clarify.

Copy link
Copy Markdown
Contributor

@afilina afilina Aug 17, 2021

Choose a reason for hiding this comment

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

Idea for description:

The function will return true if the object is either of that class, 
a child of that class, or implement that interface.

<refnamediv>
<refname>is_a</refname>
<refpurpose>Checks if the object is of this class or has this class as one of its parents</refpurpose>
<refpurpose>Checks if the object is of this class or has this class as one of its parents, or implements this interface</refpurpose>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Let me try something completely different to give us new ideas:

Suggested change
<refpurpose>Checks if the object is of this class or has this class as one of its parents, or implements this interface</refpurpose>
<refpurpose>Compares the object's type to a given class name</refpurpose>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Well, is_a() is a big mess. You can pass an object or a class/interface name as first argument (the former case should better be served by using instanceof), and there is even a third parameter which allows to prevent passing a class/interface name as first argument. How is one supposed to document that in a concise way?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Assuming the refpurpose is about the purpose of the function, not the behavior, then we merely need to state why devs might want to use it. The exact behavior and all its exceptions can be detailed within the relevant parameters.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Right, but talking about "the object" is misleading (especially since the function is mostly useful to compare class/interface names). Maybe something very general, like "Checks type relation", is appropriate as refpurpose?

Copy link
Copy Markdown
Contributor

@afilina afilina Aug 17, 2021

Choose a reason for hiding this comment

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

Some more ideas:

  • Checks type correspondence
  • Checks type analogy
  • Compares types

By the way, subclass is a viable term in the context of interfaces as well. See first sentence of LSP: https://en.wikipedia.org/wiki/Liskov_substitution_principle

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

"Subtype" might be preferable, though, but my actual with this term is that T is not a subtype/subclass of T. Isn't there are word which expresses "same type or subtype"?

Copy link
Copy Markdown
Contributor

@afilina afilina Aug 17, 2021

Choose a reason for hiding this comment

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

I can't think of such a word. Perhaps two words are still short enough: "Checks whether the object is of a given type or subtype"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm still not happy with "object", but it's overall better than before, so maybe just go with that. :)

@afilina afilina requested review from Girgias and cmb69 December 22, 2022 16:17
Girgias and others added 2 commits December 22, 2022 17:58
Co-authored-by: Anna Filina <afilina@gmail.com>
@Girgias Girgias merged commit 944af3a into php:master Dec 22, 2022
claudepache pushed a commit to claudepache/php-doc-en that referenced this pull request Jun 1, 2023
Co-authored-by: Anna Filina <afilina@gmail.com>
Co-authored-by: Christoph M. Becker <cmbecker69@gmx.de>
Co-authored-by: George Peter Banyard <girgias@php.net>
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.

4 participants