Reputation: 11
I am trying to check if my PHP class is extending another class. I am able to retrive the class itself with get_parent_class()
but when checking if that is null it's just throwing a null pointer exception.
I want to get a boolean that is true if the class is extended. Thereby avoiding the null pointer exception.
Upvotes: 1
Views: 4827
Reputation: 19372
http://php.net/manual/en/function.class-parents.php
An array on success, or FALSE on error.
function hasParents($object) {
$parents = class_parents($object);
return is_array($parents) && !empty($parents);
}
UPD.: simplified way
function hasParents($object) {
return (bool)class_parents($object);
}
https://extendsclass.com/php-bin/dfa71d0
Upvotes: 4
Reputation: 345
is_subclass_of()
function does the job:
Wisely it accepts not only class instance but also class name - no object creation needed to get the information.
Upvotes: 1