class MyClass { // some properties and methods } $reflection = new ReflectionClass('MyClass'); if ($reflection->isInstantiable()) { // the class is instantiable $obj = new MyClass(); } else { // the class is not instantiable echo "Class cannot be instantiated."; }
$reflection = new ReflectionClass('DateTime'); if ($reflection->isInstantiable()) { // the class is instantiable $now = new DateTime(); } else { // the class is not instantiable echo "Class cannot be instantiated."; }
use SomePackage\SomeClass; $reflection = new ReflectionClass(SomeClass::class); if ($reflection->isInstantiable()) { // the class is instantiable $obj = new SomeClass(); } else { // the class is not instantiable echo "Class cannot be instantiated."; }In this example, we are checking whether the `SomeClass` class from the `SomePackage` package/library can be instantiated or not. We use the `use` statement to import the class into our code and then create a `ReflectionClass` object for it. The `isInstantiable()` method is called on this object to determine if the class can be instantiated.