An example missing from the documentation is that `ReflectionClass::isInstantiable` will also return false for traits, as well as interfaces and abstract classes.
<?php
trait t {
}
$reflectionClass = new ReflectionClass("t");
var_dump($reflectionClass->isInstantiable()); ?>
As for classes with private constructors, it is still possible to create an instance by either bypassing the constructor using `ReflectionClass::newInstanceWithoutConstructor`, or by ensuring the class has a method which can create a new instance.
<?php
class p {
private function __construct() {
}
public static function create() {
return new p;
}
}
$reflectionClass = new ReflectionClass("p");
var_dump($reflectionClass->isInstantiable()); $p = p::create();
$p = $reflectionClass->newInstanceWithoutConstructor();
?>
The same is also true for protected constructors, however, the class can be instantiated from either parent or child methods, depending on where the constructor is defined.
<?php
class p {
protected function __construct() {
}
public static function create( $class = "" ) {
if (!$class) {
$class = get_called_class();
}
return new $class;
}
}
class c extends p
{
}
$p = c::create("p");
$c = p::create("c");
$reflectionClassP = new ReflectionClass("p");
$reflectionClassC = new ReflectionClass("c");
var_dump($reflectionClassP->isInstantiable()); var_dump($reflectionClassC->isInstantiable()); $p = $reflectionClassP->newInstanceWithoutConstructor();
$c = $reflectionClassC->newInstanceWithoutConstructor();
?>