ReflectionClass::isInstantiable

(PHP 5, PHP 7, PHP 8)

ReflectionClass::isInstantiable β€” ΠŸΡ€ΠΎΠ²Π΅Ρ€ΡΠ΅Ρ‚, Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ Π»ΠΈ ΡΠΎΠ·Π΄Π°Ρ‚ΡŒ экзСмпляр класса

ОписаниС

public function ReflectionClass::isInstantiable(): bool

ΠœΠ΅Ρ‚ΠΎΠ΄ провСряСт, Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ Π»ΠΈ ΡΠΎΠ·Π΄Π°Ρ‚ΡŒ экзСмпляр класса.

Бписок ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ΠΎΠ²

Π‘ΠΈΠ³Π½Π°Ρ‚ΡƒΡ€Π° Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ Π½Π΅ содСрТит ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ΠΎΠ².

Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌΡ‹Π΅ значСния

ΠœΠ΅Ρ‚ΠΎΠ΄ Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ true, Ссли Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ ΡΠΎΠ·Π΄Π°Ρ‚ΡŒ экзСмпляр класса, ΠΈΠ½Π°Ρ‡Π΅ Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ false.

ΠŸΡ€ΠΈΠΌΠ΅Ρ€Ρ‹

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #1 ΠŸΡ€ΠΈΠΌΠ΅Ρ€ ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΊΠΈ доступности инстанцирования класса ΠΌΠ΅Ρ‚ΠΎΠ΄ΠΎΠΌ ReflectionClass::isInstantiable()

<?php

class C {}

interface
iface
{
function
f1();
}

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

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

class
D extends abstractClass
{
function
f2() {}
}

trait
T
{
function
f1() {}
}

class
privateConstructor
{
private function
__construct() {}
}

$classes = array(
"C",
"iface",
"ifaceImpl",
"abstractClass",
"D",
"T",
"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)
Π’ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ Π»ΠΈ ΡΠΎΠ·Π΄Π°Ρ‚ΡŒ экзСмпляр класса T? bool(false)
Π’ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ Π»ΠΈ ΡΠΎΠ·Π΄Π°Ρ‚ΡŒ экзСмпляр класса privateConstructor? bool(false)

Π‘ΠΌΠΎΡ‚Ρ€ΠΈΡ‚Π΅ Ρ‚Π°ΠΊΠΆΠ΅

  • ReflectionClass::isInstance() - ΠŸΡ€ΠΎΠ²Π΅Ρ€ΡΠ΅Ρ‚, ΠΏΡ€ΠΈΠ½Π°Π΄Π»Π΅ΠΆΠΈΡ‚ Π»ΠΈ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ классу
οΌ‹Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΉ 1 note

up
4
shaun at slickdesign dot com dot au ΒΆ
8 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.

<?php
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.

<?php
class p {
    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 p {
    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 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();
?>