PHP 8.2.0 Alpha 2 available for testing


(PHP 5, PHP 7, PHP 8)

ReflectionClass::isInstantiableComprueba si una clase es instanciable


public ReflectionClass::isInstantiable(): bool

Comprueba si una clase es instanciable.


Esta función no tiene parámetros.

Valores devueltos

Devuelve true en caso de éxito o false en caso de error.


Ejemplo #1 Ejemplo de ReflectionClass::isInstantiable()

class { }

ifaz {

ifazImpl implements ifaz {
f1() {}

abstract class 
claseAbstracta {
f1() { }
    abstract function 

extends claseAbstracta {
f2() { }

constructorPrivado {
    private function 
__construct() { }

$clases = array(

$clases  as $clase ) {
$reflectionClass = new ReflectionClass($clase);
"¿Es instanciable la clase $clase?  ";


El resultado del ejemplo sería:

¿Es instanciable la clase C?  bool(true)
¿Es instanciable la clase ifaz?  bool(false)
¿Es instanciable la clase ifazImpl?  bool(true)
¿Es instanciable la clase claseAbstracta?  bool(false)
¿Es instanciable la clase D?  bool(true)
¿Es instanciable la clase constructorPrivado?  bool(false)

Ver también

add a note add a note

User Contributed Notes 1 note

shaun at slickdesign dot com dot au
4 years ago
An example missing from the documentation is that `ReflectionClass::isInstantiable` will also return false for traits, as well as interfaces and abstract classes.

trait t {
// Optional trait methods and properties etc.

$reflectionClass = new ReflectionClass("t");
var_dump($reflectionClass->isInstantiable()); // bool(false)

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.

class p {
    private function
__construct() {
// Optional constructor logic - not called when ReflectionClass::newInstanceWithoutConstructor is used.

    public static function
create() {
        return new

// Optional methods and properties etc.

// Class is not classed as instantiable.
$reflectionClass = new ReflectionClass("p");
var_dump($reflectionClass->isInstantiable()); // bool(false)

// We're still able to create an instance using one of the two methods.
$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.

class p {
    protected function
__construct() {
// Optional constructor logic.

    public static function
create( $class = "" ) {
        if (!
$class) {
$class = get_called_class();
        return new

// Optional parent methods and properties etc.

c extends p
// Optional child methods and properties etc.

// Both child and parent static methods have access to each other's protected constructor.
$p = c::create("p");
$c = p::create("c");

// Both are still not classed as being instantiable.
$reflectionClassP = new ReflectionClass("p");
$reflectionClassC = new ReflectionClass("c");
var_dump($reflectionClassP->isInstantiable()); // bool(false)
var_dump($reflectionClassC->isInstantiable()); // bool(false)

// We're still able to bypass the constructor and create an instance for each.
$p = $reflectionClassP->newInstanceWithoutConstructor();
$c = $reflectionClassC->newInstanceWithoutConstructor();
To Top