Keith Palmer Jr.
Keith Palmer Jr.

Reputation: 27982

In PHP 5.x, how can I detect if a class is abstract or not at run time?

I'm looping through an array of class names in PHP, fetched via get_declared_classes().

How can I check each class name to detect whether or not that particular class is an abstract class or not?

Upvotes: 22

Views: 6889

Answers (3)

vartec
vartec

Reputation: 134631

Use reflection. ReflectionClass->isAbstract()

Use it like this:

$class = new ReflectionClass('NameOfTheClass');
$abstract = $class->isAbstract();

Upvotes: 46

MrHus
MrHus

Reputation: 33388

<?php 

abstract class Picasso
{
    public function __construct()
    {

    }
} 

$class = new ReflectionClass('Picasso');

if($class->isAbstract())
{
    echo "Im abstract";
}
else
{
    echo "Im not abstract";
}

?>

See the manual : www.php.net/oop5.reflection

Upvotes: 5

jonstjohn
jonstjohn

Reputation: 60286

You can use Reflection on the class.

Upvotes: 4

Related Questions