ReflectionClass::isInstantiable

(PHP 5, PHP 7)

ReflectionClass::isInstantiableПроверяет, можно ли создать экземпляр класса

Описание

public bool ReflectionClass::isInstantiable ( void )

Проверяет, можно ли создать экземпляр класса.

Список параметров

У этой функции нет параметров.

Возвращаемые значения

Возвращает TRUE в случае успешного завершения или FALSE в случае возникновения ошибки.

Примеры

Пример #1 Пример использования ReflectionClass::isInstantiable()

<?php
class { }

interface 
iface {
    function 
f1();
}

class 
ifaceImpl implements iface {
    function 
f1() {}
}

abstract class 
abstractClass {
    function 
f1() { }
    abstract function 
f2();
}

class 
extends abstractClass {
    function 
f2() { }
}

class 
privateConstructor {
    private function 
__construct() { }
}

$classes = array(
    
"C",
    
"iface",
    
"ifaceImpl",
    
"abstractClass",
    
"D",
    
"privateConstructor",
);

foreach(
$classes  as $class ) {
    
$reflectionClass = new ReflectionClass($class);
    echo 
"Можно ли создать экземпляр класса $class?  ";
    
var_dump($reflectionClass->IsInstantiable()); 
}

?>

Результат выполнения данного примера:

Можно ли создать экземпляр класса C?  bool(true)
Можно ли создать экземпляр класса iface?  bool(false)
Можно ли создать экземпляр класса ifaceImpl?  bool(true)
Можно ли создать экземпляр класса abstractClass?  bool(false)
Можно ли создать экземпляр класса D?  bool(true)
Можно ли создать экземпляр класса privateConstructor?  bool(false)

Смотрите также

Коментарии

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 {
   
// 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.

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

    public static function 
create() {
        return new 
p;
    }

   
// 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.

<?php
class {
    protected function 
__construct() {
       
// Optional constructor logic.
   
}

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

   
// Optional parent methods and properties etc.
}

class 
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();
?>
2018-04-19 08:37:18
http://php5.kiev.ua/manual/ru/reflectionclass.isinstantiable.html

    Поддержать сайт на родительском проекте КГБ