views:

398

answers:

4

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?

+13  A: 

Use reflection. ReflectionClass->isAbstract()

Use it like this:

$class = new ReflectionClass('NameOfTheClass');
$abstract = $class->isAbstract();
vartec
beat me by 30 seconds...
Ryan Graham
Is there a way to do it *without* using ReflectionClass (so that I don't need to implement the ReflectionClass methods?)
Keith Palmer
you just pass the name of your class to ReflectionClass constructor
vartec
Ah, yes I see that now. Thanks. :-)
Keith Palmer
+3  A: 

You can use Reflection on the class.

jonstjohn
at last I'm faster ;-)
vartec
hah, I upvoted you, too. Damn, had to keep that brief to try to get first, but you win :)
jonstjohn
A: 

If you need to check this in runtime, I'd suggest you re-evaluate your application architecture.

You should never try to do this unless you're building an extremely complex application to inspect other code you cannot change.

Seb
Thanks for not answering my question.
Keith Palmer
It was just a suggestion... BTW, thanks for downvoting for suggesting something.
Seb
+2  A: 
<?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

MrHus