ReflectionClass::isAbstract

(PHP 5, PHP 7, PHP 8)

ReflectionClass::isAbstract检查类是否是抽象类(abstract)

说明

public ReflectionClass::isAbstract(): bool

检查这个类是否是抽象类(abstract)。

参数

此函数没有参数。

返回值

成功时返回 true, 或者在失败时返回 false

示例

示例 #1 ReflectionClass::isAbstract() 示例

<?php
class TestClass { }
abstract class
TestAbstractClass { }

$testClass = new ReflectionClass('TestClass');
$abstractClass = new ReflectionClass('TestAbstractClass');

var_dump($testClass->isAbstract());
var_dump($abstractClass->isAbstract());
?>

以上示例会输出:

bool(false)
bool(true)

参见

add a note add a note

User Contributed Notes 2 notes

up
3
baptiste at pillot dot fr
8 years ago
For interfaces and traits :

<?php
interface TestInterface { }
trait    
TestTrait { }

$interfaceClass = new ReflectionClass('TestInterface');
$traitClass     = new ReflectionClass('TestTrait');

var_dump($interfaceClass->isAbstract());
var_dump($traitClass->isAbstract());
?>

Using PHP versions 5.4- to 5.6, the above example will output:

bool(false)
bool(true)

Using PHP versions 7.0+, the above example will output:

bool(false)
bool(false)
up
0
baptiste at pillot dot fr
1 year ago
For traits:
- ReflectionClass::isAbstract returns true if the trait contains at least one un-implemented abstract method, including those declared into used traits.
- ReflectionClass::isAbstract returns false if all methods are implemented.

<?php
trait TI { public function has() {} }
var_dump((new ReflectionClass(TI::class))->isAbstract());

trait
TT { abstract public function has(); }
trait
T { use TT; }
var_dump((new ReflectionClass(T::class))->isAbstract());
?>
Will output:
bool(false)
bool(true)

For interfaces:
- ReflectionClass::isAbstract returns true if the interface contains at least one method, including into its extended interfaces.
- ReflectionClass::isAbstract returns false if the interface contains no method.

<?php
interface AI {}
var_dump((new ReflectionClass(AI::class))->isAbstract());

interface
II { public function has(); }
interface
I extends II {}
var_dump((new ReflectionClass(I::class))->isAbstract());
?>
Will output:
bool(false)
bool(true)

For classes:
- Reflection::isAbstract returns true if the class is marked as abstract, no matter if it contains abstract methods or not.
To Top