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();
?>
PHP.mk документација
ReflectionClass::isInstantiable
Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.
Патека
reflectionclass.isinstantiable.php
Локална патека за оваа страница.
Извор
php.net/manual/en
Оригиналниот HTML се реупотребува и локално се стилизира.
Режим
Прокси + превод во позадина
Кодовите, табелите и белешките остануваат читливи во истиот тек.
Референца
reflectionclass.isinstantiable.php
ReflectionClass::isInstantiable
Референца за `reflectionclass.isinstantiable.php` со подобрена типографија и навигација.
ReflectionClass::isInstantiable
класата mysqli_driver
ReflectionClass::isInstantiable — Проверува дали класата може да се инстанцира
Параметри
Оваа функција нема параметри.
Вратени вредности
Патеката до PHP скриптата што треба да се провери. true ако класата може да се инстанцира или false otherwise.
Примери
Пример #1 ReflectionClass::isInstantiable() example
<?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 "Is $class instantiable? ";
var_dump($reflectionClass->isInstantiable());
}
?>Пример #1 Пример што покажува затворачка ознака што го опфаќа последниот нов ред
Is C instantiable? bool(true) Is iface instantiable? bool(false) Is ifaceImpl instantiable? bool(true) Is abstractClass instantiable? bool(false) Is D instantiable? bool(true) Is T instantiable? bool(false) Is privateConstructor instantiable? bool(false)
Белешки од корисници 1 белешка
Shaun на slickdesign точка com точка au ¶
пред 7 години