0

In php, it seems that a class is not a subclass of itself

php > var_dump(is_subclass_of('Exception', 'Exception')); bool(false) php > var_dump(is_subclass_of('ErrorException', 'Exception')); bool(true) 

However, instances of Exception and ErrorException are both instances of Exception, and that is the property I want to check for. Is there a function I could replace is_subclass_of with that would make the output be true for both expressions?

5
  • 1
    class is not a subclass of itself And why should it be? Commented Feb 23, 2016 at 20:21
  • 1
    How about using OR? If it is this class OR a subclass... Commented Feb 23, 2016 at 20:23
  • 1
    php.net/manual/en/function.is-a.php Commented Feb 23, 2016 at 20:25
  • @u_mulder I never said it should be... if you read my question I want a function that returns true because instances of Exception are instances of Exception, and instances of ErrorException are instances of ErrorException Commented Feb 23, 2016 at 20:36
  • @rjdown that function requires its first argument to be an object, not a class name Commented Feb 23, 2016 at 20:37

1 Answer 1

1

Define your own function that checks whether they're the same class name or one is a subclass of the other.

function same_or_subclass_of($class, $parent) { return $class == $parent || is_subclass_of($class, $parent); } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.