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?
Class Abstraction ¶PHP has abstract classes and methods. Classes defined as abstract cannot be instantiated, and any class that contains at least one abstract method must also be abstract. Methods defined as abstract simply declare the method's signature; they cannot define the implementation.
We use abstract classes when we want to commit the programmer (either oneself or someone else) to write a certain class method, but we are only sure about the name of the method, and not the details of how it should be written. To take an example, circles, rectangles, octagons, etc.
Interface are similar to abstract classes. The difference between interfaces and abstract classes are: Interfaces cannot have properties, while abstract classes can. All interface methods must be public, while abstract class methods is public or protected.
Use reflection. ReflectionClass
->isAbstract()
Use it like this:
$class = new ReflectionClass('NameOfTheClass'); $abstract = $class->isAbstract();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With