Aaron Scherer's Blog

Checking Named Class Inheritance

Introduction

user

Aaron Scherer


php, PHP, programming

Checking Named Class Inheritance

Posted by Aaron Scherer on .
Featured

php, PHP, programming

Checking Named Class Inheritance

Posted by Aaron Scherer on .

I have been working on a new cache implementation for the Symfony2 framework, and I needed a way to check if a named class inherited a given interface , so i wrote the following:

<?php  
function classInherits( $class, $inherit ) {  
    if( is_object( $class ) ) {
        return ( $class instanceof $inherit );
    } elseif( is_string( $class ) ) {
        return (bool) in_array( $inherit, class_implements( $class ) );
    }

    throw new Exception( "First value must be the name of a valid class, or an instance of one." );
}
?>

Hope this helps!

user

Aaron Scherer

http://aaronscherer.me