简体   繁体   中英

How to check if a PHP class has a parent (Also it extends another)?

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.

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.

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM