manual-php
Version:
4,741 lines • 88.5 kB
Markdown
# 反射
* [Reflection](#reflection)
* [Reflection::getModifierNames - 获取修饰符的名称](#reflectiongetmodifiernames)
* [ReflectionClass](#reflectionclass)
* [ReflectionClass::__construct - 初始化 ReflectionClass 类](#reflectionclassconstruct)
* [ReflectionClass::getConstant - 获取类的指定常量](#reflectionclassgetconstant)
* [ReflectionClass::getConstants - 获取类的所有常量](#reflectionclassgetconstants)
* [ReflectionClass::getConstructor - 获取类的构造函数](#reflectionclassgetconstructor)
* [ReflectionClass::getDefaultProperties - 获取类的默认属性](#reflectionclassgetdefaultproperties)
* [ReflectionClass::getDocComment - 获取类的文档注释](#reflectionclassgetdoccomment)
* [ReflectionClass::getEndLine - 获取类的最后一行的行数](#reflectionclassgetendline)
* [ReflectionClass::getExtension - 根据已定义的类获取所在扩展的 ReflectionExtension 对象](#reflectionclassgetextension)
* [ReflectionClass::getExtensionName - 获取定义的类所在的扩展的名称](#reflectionclassgetextensionname)
* [ReflectionClass::getFileName - 获取定义类的文件名](#reflectionclassgetfilename)
* [ReflectionClass::getInterfaceNames - 获取类的接口名称](#reflectionclassgetinterfacenames)
* [ReflectionClass::getInterfaces - 获取类的接口](#reflectionclassgetinterfaces)
* [ReflectionClass::getMethod - 获取类的指定方法](#reflectionclassgetmethod)
* [ReflectionClass::getMethods - 获取类的方法的数组](#reflectionclassgetmethods)
* [ReflectionClass::getModifiers - 获取类的修饰符](#reflectionclassgetmodifiers)
* [ReflectionClass::getName - 获取类名](#reflectionclassgetname)
* [ReflectionClass::getNamespaceName - 获取类的命名空间的名称](#reflectionclassgetnamespacename)
* [ReflectionClass::getParentClass - 获取父类](#reflectionclassgetparentclass)
* [ReflectionClass::getProperties - 获取类的一组属性](#reflectionclassgetproperties)
* [ReflectionClass::getProperty - 获取类的指定属性](#reflectionclassgetproperty)
* [ReflectionClass::getReflectionConstant - 获取类的指定常量](#reflectionclassgetreflectionconstant)
* [ReflectionClass::getReflectionConstants - 获取类的所有常量](#reflectionclassgetreflectionconstants)
* [ReflectionClass::getShortName - 获取类的短名](#reflectionclassgetshortname)
* [ReflectionClass::getStartLine - 获取类的起始行号](#reflectionclassgetstartline)
* [ReflectionClass::getStaticProperties - 获取类的静态属性](#reflectionclassgetstaticproperties)
* [ReflectionClass::getStaticPropertyValue - 获取类的指定静态属性的值](#reflectionclassgetstaticpropertyvalue)
* [ReflectionClass::getTraitAliases - 返回 Trait 别名的一个数组](#reflectionclassgettraitaliases)
* [ReflectionClass::getTraitNames - 返回类所使用的 Trait 的名称的数组](#reflectionclassgettraitnames)
* [ReflectionClass::getTraits - 返回类所使用的 Trait 的数组](#reflectionclassgettraits)
* [ReflectionClass::hasConstant - 检查类的指定的常量是否已经定义](#reflectionclasshasconstant)
* [ReflectionClass::hasMethod - 检查类的指定的方法是否已定义](#reflectionclasshasmethod)
* [ReflectionClass::hasProperty - 检查类的指定的属性是否已定义](#reflectionclasshasproperty)
* [ReflectionClass::implementsInterface - 检查类是否实现了指定的接口](#reflectionclassimplementsinterface)
* [ReflectionClass::inNamespace - 检查类是否位于命名空间中](#reflectionclassinnamespace)
* [ReflectionClass::isAbstract - 检查类是否是抽象类](#reflectionclassisabstract)
* [ReflectionClass::isAnonymous - 检查类是否是匿名类](#reflectionclassisanonymous)
* [ReflectionClass::isCloneable - 检查类是否可复制](#reflectionclassiscloneable)
* [ReflectionClass::isFinal - 检查类是否声明为 final](#reflectionclassisfinal)
* [ReflectionClass::isInstance - 检查是否是指定类的实例](#reflectionclassisinstance)
* [ReflectionClass::isInstantiable - 检查类是否可实例化](#reflectionclassisinstantiable)
* [ReflectionClass::isInterface - 检查类是否是接口](#reflectionclassisinterface)
* [ReflectionClass::isInternal - 检查类是否由扩展或核心在内部定义](#reflectionclassisinternal)
* [ReflectionClass::isIterable - 检查类是否可迭代](#reflectionclassisiterable)
* [ReflectionClass::isIterateable - 检查类是否可迭代](#reflectionclassisiterateable)
* [ReflectionClass::isSubclassOf - 检查类是否为指定类的子类](#reflectionclassissubclassof)
* [ReflectionClass::isTrait - 检查类是否是 Trait](#reflectionclassistrait)
* [ReflectionClass::isUserDefined - 检查类是否是由用户定义的](#reflectionclassisuserdefined)
* [ReflectionClass::newInstance - 从指定的参数创建一个新的类实例](#reflectionclassnewinstance)
* [ReflectionClass::newInstanceArgs - 从指定的参数创建一个新的类实例](#reflectionclassnewinstanceargs)
* [ReflectionClass::newInstanceWithoutConstructor - 创建一个新的类实例而不调用它的构造函数](#reflectionclassnewinstancewithoutconstructor)
* [ReflectionClass::setStaticPropertyValue - 设置类的静态属性的值](#reflectionclasssetstaticpropertyvalue)
* [ReflectionClass::__toString - 返回 ReflectionClass 对象字符串的表示形式](#reflectionclasstostring)
* [ReflectionClassConstant](#reflectionclassconstant)
* [ReflectionClassConstant::__construct - 初始化 ReflectionClassConstant 类](#reflectionclassconstantconstruct)
* [ReflectionClassConstant::getDeclaringClass - 获取指定的类常量声明所在的类](#reflectionclassconstantgetdeclaringclass)
* [ReflectionClassConstant::getDocComment - 获取指定的类常量的文档注释](#reflectionclassconstantgetdoccomment)
* [ReflectionClassConstant::getModifiers - 获取指定的类常量的修饰符](#reflectionclassconstantgetmodifiers)
* [ReflectionClassConstant::getName - 获取指定的类常量名](#reflectionclassconstantgetname)
* [ReflectionClassConstant::getValue - 获取指定的类常量的值](#reflectionclassconstantgetvalue)
* [ReflectionClassConstant::isPrivate - 检查指定的类常量是否声明为 private](#reflectionclassconstantisprivate)
* [ReflectionClassConstant::isProtected - 检查指定的类常量是否声明为 protected](#reflectionclassconstantisprotected)
* [ReflectionClassConstant::isPublic - 检查指定的类常量是否声明为 public](#reflectionclassconstantispublic)
* [ReflectionClassConstant::__toString - 返回 ReflectionClassConstant 对象字符串的表示形式](#reflectionclassconstanttostring)
* [ReflectionZendExtension](#reflectionzendextension)
* [ReflectionZendExtension::__construct - 初始化 ReflectionZendExtension 类](#reflectionzendextensionconstruct)
* [ReflectionZendExtension::getAuthor - 获取 Zend 扩展的作者](#reflectionzendextensiongetauthor)
* [ReflectionZendExtension::getCopyright - 获取 Zend 扩展的版权信息](#reflectionzendextensiongetcopyright)
* [ReflectionZendExtension::getName - 获取 Zend 扩展的名称](#reflectionzendextensiongetname)
* [ReflectionZendExtension::getURL - 获取 Zend 扩展的 URL](#reflectionzendextensiongeturl)
* [ReflectionZendExtension::getVersion - 获取 Zend 扩展的版本](#reflectionzendextensiongetversion)
* [ReflectionZendExtension::__toString - 返回 ReflectionZendExtension 对象字符串的表示形式](#reflectionzendextensiontostring)
* [ReflectionExtension](#reflectionextension)
* [ReflectionExtension::__construct - 初始化 ReflectionExtension 类](#reflectionextensionconstruct)
* [ReflectionExtension::getClasses - 获取扩展中的类列表](#reflectionextensiongetclasses)
* [ReflectionExtension::getClassNames - 获取扩展中的类名称列表](#reflectionextensiongetclassnames)
* [ReflectionExtension::getConstants - 获取扩展中的类常量](#reflectionextensiongetconstants)
* [ReflectionExtension::getDependencies - 获取扩展中的依赖](#reflectionextensiongetdependencies)
* [ReflectionExtension::getFunctions - 获取扩展中的函数](#reflectionextensiongetfunctions)
* [ReflectionExtension::getINIEntries - 获取扩展在 ini 文件中的配置](#reflectionextensiongetinientries)
* [ReflectionExtension::getName - 获取扩展的名称](#reflectionextensiongetname)
* [ReflectionExtension::getVersion - 获取扩展的版本号](#reflectionextensiongetversion)
* [ReflectionExtension::info - 输出扩展的信息](#reflectionextensioninfo)
* [ReflectionExtension::isPersistent - 返回扩展是否持久化载入](#reflectionextensionispersistent)
* [ReflectionExtension::isTemporary - 返回扩展是否是临时载入](#reflectionextensionistemporary)
* [ReflectionExtension::__toString - 返回 ReflectionExtension 对象字符串的表示形式](#reflectionextensiontostring)
* [ReflectionFunction](#reflectionfunction)
* [ReflectionFunction::__construct - 初始化 ReflectionFunction 类](#reflectionfunctionconstruct)
* [ReflectionFunction::getClosure - 返回指定的函数的动态创建的闭包](#reflectionfunctiongetclosure)
* [ReflectionFunction::invoke - 调用指定的函数](#reflectionfunctioninvoke)
* [ReflectionFunction::invokeArgs - 调用指定的函数并将其参数作为数组传递](#reflectionfunctioninvokeargs)
* [ReflectionFunction::isDisabled - 检查指定的函数是否被禁用](#reflectionfunctionisdisabled)
* [ReflectionFunction::__toString - 返回 ReflectionFunction 对象字符串的表示形式](#reflectionfunctiontostring)
* [ReflectionFunctionAbstract](#reflectionfunctionabstract)
* [ReflectionFunctionAbstract::getClosureScopeClass - 返回指定的函数的与闭包关联的范围](#reflectionfunctionabstractgetclosurescopeclass)
* [ReflectionFunctionAbstract::getClosureThis - 返回指定的函数的的匿名函数](#reflectionfunctionabstractgetclosurethis)
* [ReflectionFunctionAbstract::getDocComment - 获取指定的函数的文档注释](#reflectionfunctionabstractgetdoccomment)
* [ReflectionFunctionAbstract::getEndLine - 获取指定的函数的结束行号](#reflectionfunctionabstractgetendline)
* [ReflectionFunctionAbstract::getExtension - 获取指定的函数的扩展信息](#reflectionfunctionabstractgetextension)
* [ReflectionFunctionAbstract::getExtensionName - 获取指定的函数的扩展名称](#reflectionfunctionabstractgetextensionname)
* [ReflectionFunctionAbstract::getFileName - 获取指定的函数所在的文件名称](#reflectionfunctionabstractgetfilename)
* [ReflectionFunctionAbstract::getName - 获取指定的函数的名称](#reflectionfunctionabstractgetname)
* [ReflectionFunctionAbstract::getNamespaceName - 获取指定的函数的命名空间](#reflectionfunctionabstractgetnamespacename)
* [ReflectionFunctionAbstract::getNumberOfParameters - 获取指定的函数的参数数目](#reflectionfunctionabstractgetnumberofparameters)
* [ReflectionFunctionAbstract::getNumberOfRequiredParameters - 获取指定的函数的必须输入参数的个数](#reflectionfunctionabstractgetnumberofrequiredparameters)
* [ReflectionFunctionAbstract::getParameters - 获取指定的函数的参数](#reflectionfunctionabstractgetparameters)
* [ReflectionFunctionAbstract::getReturnType - 获取指定的函数的返回类型](#reflectionfunctionabstractgetreturntype)
* [ReflectionFunctionAbstract::getShortName - 获取指定的函数的短名称](#reflectionfunctionabstractgetshortname)
* [ReflectionFunctionAbstract::getStartLine - 获取指定的函数的开始行号](#reflectionfunctionabstractgetstartline)
* [ReflectionFunctionAbstract::getStaticVariables - 获取指定的函数的静态变量](#reflectionfunctionabstractgetstaticvariables)
* [ReflectionFunctionAbstract::hasReturnType - 检查指定的函数是否声明返回类型](#reflectionfunctionabstracthasreturntype)
* [ReflectionFunctionAbstract::inNamespace - 检查指定的函数是否处于命名空间](#reflectionfunctionabstractinnamespace)
* [ReflectionFunctionAbstract::isClosure - 检查指定的函数是否是匿名函数](#reflectionfunctionabstractisclosure)
* [ReflectionFunctionAbstract::isDeprecated - 检查指定的函数是否已经弃用](#reflectionfunctionabstractisdeprecated)
* [ReflectionFunctionAbstract::isGenerator - 检查指定的函数是否是生成器](#reflectionfunctionabstractisgenerator)
* [ReflectionFunctionAbstract::isInternal - 检查指定的函数是否是内置函数](#reflectionfunctionabstractisinternal)
* [ReflectionFunctionAbstract::isUserDefined - 检查指定的函数是否是用户自定义](#reflectionfunctionabstractisuserdefined)
* [ReflectionFunctionAbstract::isVariadic - 检查指定的函数是否是可变函数](#reflectionfunctionabstractisvariadic)
* [ReflectionFunctionAbstract::returnsReference - 检查指定的函数是否返回引用](#reflectionfunctionabstractreturnsreference)
* [ReflectionMethod](#reflectionmethod)
* [ReflectionMethod::__construct - 初始化 ReflectionMethod 类](#reflectionmethodconstruct)
* [ReflectionMethod::getClosure - 返回一个动态建立的方法调用接口,可以直接调用非公开方法](#reflectionmethodgetclosure)
* [ReflectionMethod::getDeclaringClass - 获取指定的类方法声明所在的类](#reflectionmethodgetdeclaringclass)
* [ReflectionMethod::getModifiers - 获取指定的类方法的修饰符](#reflectionmethodgetmodifiers)
* [ReflectionMethod::getPrototype - 返回指定的类方法的原型](#reflectionmethodgetprototype)
* [ReflectionMethod::invoke - 调用指定的类方法](#reflectionmethodinvoke)
* [ReflectionMethod::invokeArgs - 调用指定的类方法](#reflectionmethodinvokeargs)
* [ReflectionMethod::isAbstract - 判断指定的类方法是否是抽象方法](#reflectionmethodisabstract)
* [ReflectionMethod::isConstructor - 判断指定的类方法是否是构造方法](#reflectionmethodisconstructor)
* [ReflectionMethod::isDestructor - 判断指定的类方法是否是析构方法](#reflectionmethodisdestructor)
* [ReflectionMethod::isFinal - 判断指定的类方法是否声明为 final](#reflectionmethodisfinal)
* [ReflectionMethod::isPrivate - 判断指定的类方法是否声明为 private](#reflectionmethodisprivate)
* [ReflectionMethod::isProtected - 判断指定的类方法是否声明为 protected](#reflectionmethodisprotected)
* [ReflectionMethod::isPublic - 判断指定的类方法是否声明为 public](#reflectionmethodispublic)
* [ReflectionMethod::isStatic - 判断指定的类方法是否声明为 static](#reflectionmethodisstatic)
* [ReflectionMethod::setAccessible - 设置指定的类方法是否可以访问](#reflectionmethodsetaccessible)
* [ReflectionMethod::__toString - 返回 ReflectionMethod 对象字符串的表示形式](#reflectionmethodtostring)
* [ReflectionObject](#reflectionobject)
* [ReflectionObject::__construct - 初始化 ReflectionObject 类](#reflectionobjectconstruct)
* [ReflectionParameter](#reflectionparameter)
* [ReflectionParameter::__construct - 初始化 ReflectionParameter 类](#reflectionparameterconstruct)
* [ReflectionParameter::allowsNull - 检查指定的参数是否允许为空](#reflectionparameterallowsnull)
* [ReflectionParameter::canBePassedByValue - 返回指定的参数是否可以按值传递](#reflectionparametercanbepassedbyvalue)
* [ReflectionParameter::getClass - 获得指定的参数的类型提示的类](#reflectionparametergetclass)
* [ReflectionParameter::getDeclaringClass - 返回指定的参数声明所在的类](#reflectionparametergetdeclaringclass)
* [ReflectionParameter::getDeclaringFunction - 返回指定的参数声明所在的函数](#reflectionparametergetdeclaringfunction)
* [ReflectionParameter::getDefaultValue - 返回指定的参数的默认值](#reflectionparametergetdefaultvalue)
* [ReflectionParameter::getDefaultValueConstantName - 返回指定的参数的默认值的常量名称](#reflectionparametergetdefaultvalueconstantname)
* [ReflectionParameter::getName - 获取指定的参数的名称](#reflectionparametergetname)
* [ReflectionParameter::getPosition - 获取指定的参数的位置](#reflectionparametergetposition)
* [ReflectionParameter::getType - 获取指定的参数的类型](#reflectionparametergettype)
* [ReflectionParameter::hasType - 检查指定的参数是否存在类型声明](#reflectionparameterhastype)
* [ReflectionParameter::isArray - 检查指定的参数是否是数组](#reflectionparameterisarray)
* [ReflectionParameter::isCallable - 检查指定的参数是否是可调用型](#reflectionparameteriscallable)
* [ReflectionParameter::isDefaultValueAvailable - 检查指定的参数是否有默认值](#reflectionparameterisdefaultvalueavailable)
* [ReflectionParameter::isDefaultValueConstant - 检查指定的参数的默认是是否是常量](#reflectionparameterisdefaultvalueconstant)
* [ReflectionParameter::isOptional - 检查指定的参数是否是可选的](#reflectionparameterisoptional)
* [ReflectionParameter::isPassedByReference - 返回指定的参数是否可以按引用传递](#reflectionparameterispassedbyreference)
* [ReflectionParameter::isVariadic - 返回指定的参数是否是可变参数](#reflectionparameterisvariadic)
* [ReflectionParameter::__toString - 返回 ReflectionParameter 对象字符串的表示形式](#reflectionparametertostring)
* [ReflectionProperty](#reflectionproperty)
* [ReflectionProperty::__construct - 初始化 ReflectionProperty 类](#reflectionpropertyconstruct)
* [ReflectionProperty::getDeclaringClass - 获取指定的类属性声明所在的类](#reflectionpropertygetdeclaringclass)
* [ReflectionProperty::getDocComment - 获取指定类属性的文档注释](#reflectionpropertygetdoccomment)
* [ReflectionProperty::getModifiers - 获取指定类属性的修饰符](#reflectionpropertygetmodifiers)
* [ReflectionProperty::getName - 获取指定类属性的名称](#reflectionpropertygetname)
* [ReflectionProperty::getType - 获取指定类属性的类型](#reflectionpropertygettype)
* [ReflectionProperty::getValue - 获取指定类属性的值](#reflectionpropertygetvalue)
* [ReflectionProperty::hasType - 检查指定的类属性是否存在类型声明](#reflectionpropertyhastype)
* [ReflectionProperty::isDefault - 检查指定的类属性是否是默认属性](#reflectionpropertyisdefault)
* [ReflectionProperty::isInitialized - 检查指定的类属性是否已初始化](#reflectionpropertyisinitialized)
* [ReflectionProperty::isPrivate - 检查指定的类属性是否声明为 private](#reflectionpropertyisprivate)
* [ReflectionProperty::isProtected - 检查指定的类属性是否声明为 protected](#reflectionpropertyisprotected)
* [ReflectionProperty::isPublic - 检查指定的类属性是否声明为 public](#reflectionpropertyispublic)
* [ReflectionProperty::isStatic - 检查指定的类属性是否声明为 static](#reflectionpropertyisstatic)
* [ReflectionProperty::setAccessible - 设置指定的类属性是否可以访问](#reflectionpropertysetaccessible)
* [ReflectionProperty::setValue - 设置指定的类属性的值](#reflectionpropertysetvalue)
* [ReflectionProperty::__toString - 返回 ReflectionProperty 对象字符串的表示形式](#reflectionpropertytostring)
* [ReflectionType](#ReflectionType)
* [ReflectionType::allowsNull - 检查指定的类型是否允许为空](#reflectiontypeallowsnull)
* [ReflectionType::isBuiltin - 检查指定的类型是否是内置类型](#reflectiontypeisbuiltin)
* [ReflectionGenerator](#reflectiongenerator)
* [ReflectionGenerator::__construct - 初始化 ReflectionGenerator 类](#reflectiongeneratorconstruct)
* [ReflectionGenerator::getExecutingFile - 获取指定的生成器所在的文件名](#reflectiongeneratorgetexecutingfile)
* [ReflectionGenerator::getExecutingGenerator - 获取指定的生成器对象](#reflectiongeneratorgetexecutinggenerator)
* [ReflectionGenerator::getExecutingLine - 获取指定的生成器所在的行号](#reflectiongeneratorgetexecutingline)
* [ReflectionGenerator::getFunction - 获取指定的生成器的函数名称](#reflectiongeneratorgetfunction)
* [ReflectionGenerator::getThis - 获取指定的生成器的 $this 的值](#reflectiongeneratorgetthis)
* [ReflectionGenerator::getTrace - 获取指定的生成器的堆栈跟踪信息](#reflectiongeneratorgettrace)
## Reflection
### Reflection::getModifierNames
```php
<?php
class Foo
{
/**
* Public method.
*
* @param void
* @return void
*/
final public function public()
{
//
}
/**
* Protected method.
*
* @param void
* @return void
*/
protected static function protected()
{
//
}
/**
* Private method.
*
* @param void
* @return void
*/
private function private()
{
//
}
}
var_dump(Reflection::getModifierNames((new ReflectionMethod('Foo', 'public'))->getModifiers())); // array(2) { [0]=> string(5) "final" [1]=> string(6) "public" }
var_dump(Reflection::getModifierNames((new ReflectionMethod('Foo', 'protected'))->getModifiers())); // array(2) { [0]=> string(9) "protected" [1]=> string(6) "static" }
var_dump(Reflection::getModifierNames((new ReflectionMethod('Foo', 'private'))->getModifiers())); // array(1) { [0]=> string(7) "private" }
```
## ReflectionClass
### ReflectionClass::__construct
```php
<?php
class Foo
{
//
}
var_dump(new ReflectionClass('Foo')); // object(ReflectionClass)#1 (1) { ["name"]=> string(3) "Foo" }
var_dump(new ReflectionClass(new Foo())); // object(ReflectionClass)#1 (1) { ["name"]=> string(3) "Foo" }
```
### ReflectionClass::getConstant
```php
<?php
class Foo
{
/**
* Just a test constant.
*
* @var string
*/
public const CONSTANT = 'foo';
}
$foo = new ReflectionClass('Foo');
var_dump($foo->getConstant('constant')); // bool(false)
var_dump($foo->getConstant('CONSTANT')); // string(3) "foo"
```
### ReflectionClass::getConstants
```php
<?php
class Foo
{
/**
* Just a test constant.
*
* @var string
*/
public const CONSTANT = 'foo';
}
$foo = new ReflectionClass('Foo');
var_dump($foo->getConstants()); // array(1) { ["CONSTANT"]=> string(3) "foo" }
```
### ReflectionClass::getConstructor
```php
<?php
class Foo
{
/**
* Just a test constructor.
*
* @param void
* @return void
*/
public function __construct()
{
//
}
}
$foo = new ReflectionClass('Foo');
var_dump($foo->getConstructor()); // object(ReflectionMethod)#2 (2) { ["name"]=> string(11) "__construct" ["class"]=> string(3) "Foo" }
```
### ReflectionClass::getDefaultProperties
```php
<?php
class Foo
{
/**
* Just a test property.
*
* @var string
*/
public string $foo = 'foo';
/**
* Just a test property.
*
* @var string
*/
protected string $bar = 'bar';
/**
* Just a test property.
*
* @var string|null
*/
private ?string $baz;
/**
* Just a test property.
*
* @var string
*/
public static string $qux = 'qux';
}
$foo = new ReflectionClass('Foo');
var_dump($foo->getDefaultProperties()); // array(3) { ["qux"]=> string(3) "qux" ["foo"]=> string(3) "foo" ["bar"]=> string(3) "bar" }
```
### ReflectionClass::getDocComment
```php
<?php
/**
* Just a test class.
*/
class Foo
{
//
}
$foo = new ReflectionClass('Foo');
var_dump($foo->getDocComment());
// string(29) "/**
// * Just a test class.
// */"
```
### ReflectionClass::getEndLine
```php
<?php
class Foo
{
//
}
$foo = new ReflectionClass('Foo');
var_dump($foo->getEndLine()); // int(6)
```
### ReflectionClass::getExtension
```php
<?php
$foo = new ReflectionClass('ReflectionClass');
var_dump($foo->getExtension()); // object(ReflectionExtension)#2 (1) { ["name"]=> string(10) "Reflection" }
```
### ReflectionClass::getExtensionName
```php
<?php
$foo = new ReflectionClass('ReflectionClass');
var_dump($foo->getExtensionName()); // string(10) "Reflection"
```
### ReflectionClass::getFileName
```php
<?php
class Foo
{
//
}
$foo = new ReflectionClass('Foo');
var_dump($foo->getFileName());
```
### ReflectionClass::getInterfaceNames
```php
<?php
interface Foo
{
//
}
interface Bar
{
//
}
class Baz implements Foo, Bar
{
//
}
$baz = new ReflectionClass('Baz');
var_dump($baz->getInterfaceNames()); // array(2) { [0]=> string(3) "Foo" [1]=> string(3) "Bar" }
```
### ReflectionClass::getInterfaces
```php
<?php
interface Foo
{
//
}
interface Bar
{
//
}
class Baz implements Foo, Bar
{
//
}
$baz = new ReflectionClass('Baz');
var_dump($baz->getInterfaces()); // array(2) { ["Foo"]=> object(ReflectionClass)#2 (1) { ["name"]=> string(3) "Foo" } ["Bar"]=> object(ReflectionClass)#3 (1) { ["name"]=> string(3) "Bar" } }
```
### ReflectionClass::getMethod
```php
<?php
class Foo
{
/**
* Just a test method.
*
* @param void
* @return void
*/
public function method()
{
//
}
}
$foo = new ReflectionClass('Foo');
var_dump($foo->getMethod('method')); // object(ReflectionMethod)#2 (2) { ["name"]=> string(6) "method" ["class"]=> string(3) "Foo" }
```
### ReflectionClass::getMethods
```php
<?php
class Foo
{
/**
* Just a test method.
*
* @param void
* @return void
*/
public function method()
{
//
}
}
$foo = new ReflectionClass('Foo');
var_dump($foo->getMethods()); // array(1) { [0]=> object(ReflectionMethod)#2 (2) { ["name"]=> string(6) "method" ["class"]=> string(3) "Foo" } }
```
### ReflectionClass::getModifiers
```php
<?php
class Foo
{
//
}
$foo = new ReflectionClass('Foo');
var_dump($foo->getModifiers()); // int(0)
```
### ReflectionClass::getName
```php
<?php
namespace Foo;
class Bar
{
//
}
$foo = new \ReflectionClass('stdClass');
var_dump($foo->getName()); // string(8) "stdClass"
$bar = new \ReflectionClass(Bar::class);
var_dump($bar->getName()); // string(7) "Foo\Bar"
```
### ReflectionClass::getNamespaceName
```php
<?php
namespace Foo;
class Bar
{
//
}
$foo = new \ReflectionClass('stdClass');
var_dump($foo->getNamespaceName()); // string(0) ""
$bar = new \ReflectionClass(Bar::class);
var_dump($bar->getNamespaceName()); // string(3) "Foo"
```
### ReflectionClass::getParentClass
```php
<?php
class Foo
{
//
}
class Bar extends Foo
{
//
}
$bar = new ReflectionClass('Bar');
var_dump($bar->getParentClass()); // object(ReflectionClass)#2 (1) { ["name"]=> string(3) "Foo" }
```
### ReflectionClass::getProperties
```php
<?php
class Foo
{
/**
* Just a test property.
*
* @var string
*/
public string $foo = 'foo';
/**
* Just a test property.
*
* @var string
*/
protected string $bar = 'bar';
/**
* Just a test property.
*
* @var string
*/
private string $baz = 'baz';
}
$foo = new ReflectionClass('Foo');
var_dump($foo->getProperties()); // array(3) { [0]=> object(ReflectionProperty)#2 (2) { ["name"]=> string(3) "foo" ["class"]=> string(3) "Foo" } [1]=> object(ReflectionProperty)#3 (2) { ["name"]=> string(3) "bar" ["class"]=> string(3) "Foo" } [2]=> object(ReflectionProperty)#4 (2) { ["name"]=> string(3) "baz" ["class"]=> string(3) "Foo" } }
var_dump($foo->getProperties(ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED)); // array(2) { [0]=> object(ReflectionProperty)#4 (2) { ["name"]=> string(3) "foo" ["class"]=> string(3) "Foo" } [1]=> object(ReflectionProperty)#3 (2) { ["name"]=> string(3) "bar" ["class"]=> string(3) "Foo" } }
```
### ReflectionClass::getProperty
```php
<?php
class Foo
{
/**
* Just a test property.
*
* @var string
*/
public string $foo = 'foo';
/**
* Just a test property.
*
* @var string
*/
protected string $bar = 'bar';
/**
* Just a test property.
*
* @var string
*/
private string $baz = 'baz';
}
$foo = new ReflectionClass('Foo');
var_dump($foo->getProperty('foo')); // object(ReflectionProperty)#2 (2) { ["name"]=> string(3) "foo" ["class"]=> string(3) "Foo" }
var_dump($foo->getProperty('bar')); // object(ReflectionProperty)#2 (2) { ["name"]=> string(3) "bar" ["class"]=> string(3) "Foo" }
var_dump($foo->getProperty('baz')); // object(ReflectionProperty)#2 (2) { ["name"]=> string(3) "baz" ["class"]=> string(3) "Foo" }
```
### ReflectionClass::getReflectionConstant
```php
<?php
class Foo
{
/**
* Just a test constant.
*
* @var string
*/
public const CONSTANT = 'foo';
}
$foo = new ReflectionClass('Foo');
var_dump($foo->getReflectionConstant('constant')); // bool(false)
var_dump($foo->getReflectionConstant('CONSTANT')); // object(ReflectionClassConstant)#2 (2) { ["name"]=> string(8) "CONSTANT" ["class"]=> string(3) "Foo" }
```
### ReflectionClass::getReflectionConstants
```php
<?php
class Foo
{
/**
* Just a test constant.
*
* @var string
*/
public const CONSTANT = 'foo';
}
$foo = new ReflectionClass('Foo');
var_dump($foo->getReflectionConstants()); // array(1) { [0]=> object(ReflectionClassConstant)#2 (2) { ["name"]=> string(8) "CONSTANT" ["class"]=> string(3) "Foo" } }
```
### ReflectionClass::getShortName
```php
<?php
namespace Foo;
class Bar
{
//
}
$foo = new \ReflectionClass('stdClass');
var_dump($foo->getShortName()); // string(8) "stdClass"
$bar = new \ReflectionClass('Foo\Bar');
var_dump($bar->getShortName()); // string(3) "Bar"
```
### ReflectionClass::getStartLine
```php
<?php
class Foo
{
//
}
$foo = new ReflectionClass('Foo');
var_dump($foo->getStartLine()); // int(3)
```
### ReflectionClass::getStaticProperties
```php
<?php
class Foo
{
/**
* Just a test property.
*
* @var string
*/
public string $foo = 'foo';
/**
* Just a test property.
*
* @var string
*/
protected string $bar = 'bar';
/**
* Just a test property.
*
* @var string|null
*/
private ?string $baz;
/**
* Just a test property.
*
* @var string
*/
public static string $qux = 'qux';
}
$foo = new ReflectionClass('Foo');
var_dump($foo->getStaticProperties()); // array(1) { ["qux"]=> string(3) "qux" }
```
### ReflectionClass::getStaticPropertyValue
```php
<?php
class Foo
{
/**
* Just a test property.
*
* @var string
*/
public static string $foo = 'foo';
}
$foo = new ReflectionClass('Foo');
var_dump($foo->getStaticPropertyValue('foo')); // string(3) "foo"
```
### ReflectionClass::getTraitAliases
```php
<?php
trait Foo
{
/**
* Just a test method.
*
* @param void
* @return void
*/
public function method()
{
//
}
}
class Bar
{
use Foo
{
Foo::method as test;
}
}
$bar = new ReflectionClass('Bar');
var_dump($bar->getTraitAliases()); // array(1) { ["test"]=> string(11) "Foo::method" }
```
### ReflectionClass::getTraitNames
```php
<?php
trait Foo
{
//
}
trait Bar
{
//
}
class Baz
{
use Foo, Bar;
}
$baz = new ReflectionClass('Baz');
var_dump($baz->getTraitNames()); // array(2) { [0]=> string(3) "Foo" [1]=> string(3) "Bar" }
```
### ReflectionClass::getTraits
```php
<?php
trait Foo
{
//
}
trait Bar
{
//
}
class Baz
{
use Foo, Bar;
}
$baz = new ReflectionClass('Baz');
var_dump($baz->getTraits()); // array(2) { ["Foo"]=> object(ReflectionClass)#2 (1) { ["name"]=> string(3) "Foo" } ["Bar"]=> object(ReflectionClass)#3 (1) { ["name"]=> string(3) "Bar" } }
```
### ReflectionClass::hasConstant
```php
<?php
class Foo
{
/**
* Just a test constant.
*
* @var string
*/
public const CONSTANT = 'foo';
}
$foo = new ReflectionClass('Foo');
var_dump($foo->hasConstant('constant')); // bool(false)
var_dump($foo->hasConstant('CONSTANT')); // bool(true)
```
### ReflectionClass::hasMethod
```php
<?php
class Foo
{
/**
* Public method.
*
* @param void
* @return void
*/
public function public()
{
//
}
/**
* Protected method.
*
* @param void
* @return void
*/
protected function protected()
{
//
}
/**
* Private method.
*
* @param void
* @return void
*/
private function private()
{
//
}
}
$foo = new ReflectionClass('Foo');
var_dump($foo->hasMethod('public')); // bool(true)
var_dump($foo->hasMethod('protected')); // bool(true)
var_dump($foo->hasMethod('private')); // bool(true)
var_dump($foo->hasMethod('method')); // bool(false)
```
### ReflectionClass::hasProperty
```php
<?php
class Foo
{
/**
* Just a test property.
*
* @var string
*/
public string $foo = 'foo';
/**
* Just a test property.
*
* @var string
*/
protected string $bar = 'bar';
/**
* Just a test property.
*
* @var string
*/
private string $baz = 'baz';
}
$foo = new ReflectionClass('Foo');
var_dump($foo->hasProperty('foo')); // bool(true)
var_dump($foo->hasProperty('bar')); // bool(true)
var_dump($foo->hasProperty('baz')); // bool(true)
var_dump($foo->hasProperty('property')); // bool(false)
```
### ReflectionClass::implementsInterface
```php
<?php
interface Foo
{
//
}
interface Bar
{
//
}
class Baz implements Foo
{
//
}
$baz = new ReflectionClass('Baz');
var_dump($baz->implementsInterface('Foo')); // bool(true)
var_dump($baz->implementsInterface('Bar')); // bool(false)
```
### ReflectionClass::inNamespace
```php
<?php
namespace Foo;
class Bar
{
//
}
$foo = new \ReflectionClass('stdClass');
var_dump($foo->inNamespace()); // bool(false)
$bar = new \ReflectionClass(Bar::class);
var_dump($bar->inNamespace()); // bool(true)
```
### ReflectionClass::isAbstract
```php
<?php
class Foo
{
//
}
abstract class Bar
{
//
}
$foo = new ReflectionClass('Foo');
var_dump($foo->isAbstract()); // bool(false)
$bar = new ReflectionClass('Bar');
var_dump($bar->isAbstract()); // bool(true)
```
### ReflectionClass::isAnonymous
```php
<?php
class Foo
{
//
}
$foo = new ReflectionClass('Foo');
var_dump($foo->isAnonymous()); // bool(false)
$bar = new class {
//
};
$bar = new ReflectionClass($bar);
var_dump($bar->isAnonymous()); // bool(true)
```
### ReflectionClass::isCloneable
```php
<?php
class Foo
{
//
}
class Bar
{
/**
* Do not clone this class.
*
* @param void
* @return void
*/
private function __clone()
{
//
}
}
$foo = new ReflectionClass('Foo');
var_dump($foo->isCloneable()); // bool(true)
$bar = new ReflectionClass('Bar');
var_dump($bar->isCloneable()); // bool(false)
```
### ReflectionClass::isFinal
```php
<?php
class Foo
{
//
}
final class Bar
{
//
}
$foo = new ReflectionClass('Foo');
var_dump($foo->isFinal()); // bool(false)
$bar = new ReflectionClass('Bar');
var_dump($bar->isFinal()); // bool(true)
```
### ReflectionClass::isInstance
```php
<?php
class Foo
{
//
}
$foo = new ReflectionClass('Foo');
var_dump($foo->isInstance(new Foo())); // bool(true)
var_dump((new Foo()) instanceof Foo); // bool(true)
var_dump(is_a(new Foo(), 'Foo')); // bool(true)
```
### ReflectionClass::isInstantiable
```php
<?php
class Foo
{
//
}
interface Bar
{
//
}
abstract class Baz
{
//
}
trait Qux
{
//
}
class Quux
{
/**
* Do not instantiate this class.
*
* @param void
* @return void
*/
private function __construct()
{
//
}
}
$foo = new ReflectionClass('Foo');
var_dump($foo->isInstantiable()); // bool(true)
$bar = new ReflectionClass('Bar');
var_dump($bar->isInstantiable()); // bool(false)
$baz = new ReflectionClass('Baz');
var_dump($baz->isInstantiable()); // bool(false)
$qux = new ReflectionClass('Qux');
var_dump($qux->isInstantiable()); // bool(false)
$quux = new ReflectionClass('Quux');
var_dump($quux->isInstantiable()); // bool(false)
```
### ReflectionClass::isInterface
```php
<?php
class Foo
{
//
}
interface Bar
{
//
}
$foo = new ReflectionClass('Foo');
var_dump($foo->isInterface()); // bool(false)
$bar = new ReflectionClass('Bar');
var_dump($bar->isInterface()); // bool(true)
```
### ReflectionClass::isInternal
```php
<?php
class Bar
{
//
}
$foo = new ReflectionClass('stdClass');
var_dump($foo->isInternal()); // bool(true)
$bar = new ReflectionClass('Bar');
var_dump($bar->isInternal()); // bool(false)
```
### ReflectionClass::isIterable
```php
<?php
class Foo
{
//
}
class Bar implements Iterator
{
/**
* Store an array.
*
* @var array
*/
private array $property;
/**
* Store an array to the property.
*
* @param array $array
* @return void
*/
public function __construct(array $array)
{
$this->property = $array;
}
/**
* Return the current element of the specified property.
*
* @param void
* @return int
*/
public function current(): int
{
var_dump(__METHOD__);
return current($this->property);
}
/**
* Advance the internal pointer of the specified property.
*
* @param void
* @return void
*/
public function next()
{
var_dump(__METHOD__);
next($this->property);
}
/**
* Return the current key of the specified property.
*
* @param void
* @return int
*/
public function key(): int
{
var_dump(__METHOD__);
return key($this->property);
}
/**
* Return the validity of the current position of the specified property.
*
* @param void
* @return bool
*/
public function valid(): bool
{
var_dump(__METHOD__);
return current($this->property) !== false;
}
/**
* Set the internal pointer of the specified property to the first element.
*
* @param void
* @return void
*/
public function rewind()
{
var_dump(__METHOD__);
reset($this->property);
}
}
class Baz implements IteratorAggregate
{
/**
* Just a test property.
*
* @var string
*/
public string $foo = 'foo';
/**
* Just a test property.
*
* @var string
*/
public string $bar = 'bar';
/**
* Just a test property.
*
* @var string
*/
public string $baz = 'baz';
/**
* Just a test property.
*
* @var string
*/
public string $qux = 'qux';
/**
* Return an external iterator.
*
* @param void
* @return ArrayIterator
*/
public function getIterator(): ArrayIterator
{
return new ArrayIterator($this);
}
}
class Qux extends ArrayObject
{
//
}
$foo = new ReflectionClass('Foo');
var_dump($foo->isIterable()); // bool(false)
$bar = new ReflectionClass('Bar');
var_dump($bar->isIterable()); // bool(true)
$baz = new ReflectionClass('Baz');
var_dump($baz->isIterable()); // bool(true)
$qux = new ReflectionClass('Qux');
var_dump($qux->isIterable()); // bool(true)
```
### ReflectionClass::isIterateable
```php
<?php
class Foo
{
//
}
class Bar implements Iterator
{
/**
* Store an array.
*
* @var array
*/
private array $property;
/**
* Store an array to the property.
*
* @param array $array
* @return void
*/
public function __construct(array $array)
{
$this->property = $array;
}
/**
* Return the current element of the specified property.
*
* @param void
* @return int
*/
public function current(): int
{
var_dump(__METHOD__);
return current($this->property);
}
/**
* Advance the internal pointer of the specified property.
*
* @param void
* @return void
*/
public function next()
{
var_dump(__METHOD__);
next($this->property);
}
/**
* Return the current key of the specified property.
*
* @param void
* @return int
*/
public function key(): int
{
var_dump(__METHOD__);
return key($this->property);
}
/**
* Return the validity of the current position of the specified property.
*
* @param void
* @return bool
*/
public function valid(): bool
{
var_dump(__METHOD__);
return current($this->property) !== false;
}
/**
* Set the internal pointer of the specified property to the first element.
*
* @param void
* @return void
*/
public function rewind()
{
var_dump(__METHOD__);
reset($this->property);
}
}
class Baz implements IteratorAggregate
{
/**
* Just a test property.
*
* @var string
*/
public string $foo = 'foo';
/**
* Just a test property.
*
* @var string
*/
public string $bar = 'bar';
/**
* Just a test property.
*
* @var string
*/
public string $baz = 'baz';
/**
* Just a test property.
*
* @var string
*/
public string $qux = 'qux';
/**
* Return an external iterator.
*
* @param void
* @return ArrayIterator
*/
public function getIterator(): ArrayIterator
{
return new ArrayIterator($this);
}
}
class Qux extends ArrayObject
{
//
}
$foo = new ReflectionClass('Foo');
var_dump($foo->isIterateable()); // bool(false)
$bar = new ReflectionClass('Bar');
var_dump($bar->isIterateable()); // bool(true)
$baz = new ReflectionClass('Baz');
var_dump($baz->isIterateable()); // bool(true)
$qux = new ReflectionClass('Qux');
var_dump($qux->isIterateable()); // bool(true)
```
### ReflectionClass::isSubclassOf
```php
<?php
class Foo
{
//
}
class Bar
{
//
}
class Baz extends Bar
{
//
}
$baz = new ReflectionClass('Baz');
var_dump($baz->isSubclassOf('Bar')); // bool(true)
var_dump($baz->isSubclassOf('Foo')); // bool(false)
```
### ReflectionClass::isTrait
```php
<?php
class Foo
{
//
}
trait Bar
{
//
}
$foo = new ReflectionClass('Foo');
var_dump($foo->isTrait()); // bool(false)
$bar = new ReflectionClass('Bar');
var_dump($bar->isTrait()); // bool(true)
```
### ReflectionClass::isUserDefined
```php
<?php
class Bar
{
//
}
$foo = new ReflectionClass('stdClass');
var_dump($foo->isUserDefined()); // bool(false)
$bar = new ReflectionClass('Bar');
var_dump($bar->isUserDefined()); // bool(true)
```
### ReflectionClass::newInstance
```php
<?php
class Foo
{
/**
* Just a test property.
*
* @var string|null
*/
public ?string $foo;
/**
* Just a test property.
*
* @var string|null
*/
public ?string $bar;
/**
* Initialize the value of the property.
*
* @param string|null $foo
* @param string|null $bar
* @return void
*/
public function __construct(?string $foo = null, ?string $bar = null)
{
$this->foo = $foo;
$this->bar = $bar;
}
}
$foo = new ReflectionClass('Foo');
$foo = $foo->newInstance();
var_dump($foo->foo); // NULL
var_dump($foo->bar); // NULL
$foo = new ReflectionClass('Foo');
$foo = $foo->newInstance('foo');
var_dump($foo->foo); // string(3) "foo"
var_dump($foo->bar); // NULL
$foo = new ReflectionClass('Foo');
$foo = $foo->newInstance('foo', 'bar');
var_dump($foo->foo); // string(3) "foo"
var_dump($foo->bar); // string(3) "bar"
```
### ReflectionClass::newInstanceArgs
```php
<?php
class Foo
{
/**
* Just a test property.
*
* @var string|null
*/
public ?string $foo;
/**
* Just a test property.
*
* @var string|null
*/
public ?string $bar;
/**
* Initialize the value of the property.
*
* @param string|null $value
* @return void
*/
public function __construct(?string ...$value)
{
$this->foo = isset($value[0]) ? $value[0] : null;
$this->bar = isset($value[1]) ? $value[1] : null;
}
}
$foo = new ReflectionClass('Foo');
$foo = $foo->newInstanceArgs();
var_dump($foo->foo); // NULL
var_dump($foo->bar); // NULL
$foo = new ReflectionClass('Foo');
$foo = $foo->newInstanceArgs(['foo']);
var_dump($foo->foo); // string(3) "foo"
var_dump($foo->bar); // NULL
$foo = new ReflectionClass('Foo');
$foo = $foo->newInstanceArgs(['foo', 'bar']);
var_dump($foo->foo); // string(3) "foo"
var_dump($foo->bar); // string(3) "bar"
```
### ReflectionClass::newInstanceWithoutConstructor
```php
<?php
class Foo
{
/**
* Just a test property.
*
* @var string
*/
public string $foo = 'foo';
/**
* Just a test property.
*
* @var string
*/
public string $bar = 'bar';
/**
* Change the value of the property.
*
* @param void
* @return void
*/
public function __construct()
{
list($this->foo, $this->bar) = ['FOO', 'BAR'];
}
}
$foo = new ReflectionClass('Foo');
$foo = $foo->newInstanceWithoutConstructor();
var_dump($foo->foo); // string(3) "foo"
var_dump($foo->bar); // string(3) "bar"
```
### ReflectionClass::setStaticPropertyValue
```php
<?php
class Foo
{
/**
* Just a test property.
*
* @var string
*/
public static string $foo = 'foo';
}
$foo = new ReflectionClass('Foo');
$foo->setStaticPropertyValue('foo', 'bar');
var_dump((new Foo())::$foo); // string(3) "bar"
```
### ReflectionClass::__toString
```php
<?php
class Foo
{
//
}
$foo = new ReflectionClass('Foo');
echo $foo;
print $foo;
```
## ReflectionClassConstant
### ReflectionClassConstant::__construct
```php
<?php
class Foo
{
/**
* Just a test constant.
*
* @var string
*/
public const CONSTANT = 'foo';
}
var_dump(new ReflectionClassConstant('Foo', 'CONSTANT')); // object(ReflectionClassConstant)#1 (2) { ["name"]=> string(8) "CONSTANT" ["class"]=> string(3) "Foo" }
```
### ReflectionClassConstant::getDeclaringClass
```php
<?php
class Foo
{
/**
* Just a test constant.
*
* @var string
*/
public const CONSTANT = 'foo';
}
$foo = new ReflectionClassConstant('Foo', 'CONSTANT');
var_dump($foo->getDeclaringClass()); // object(ReflectionClass)#2 (1) { ["name"]=> string(3) "Foo" }
```
### ReflectionClassConstant::getDocComment
```php
<?php
class Foo
{
/**
* Just a test constant.
*
* @var string
*/
public const CONSTANT = 'foo';
}
$foo = new ReflectionClassConstant('Foo', 'CONSTANT');
var_dump($foo->getDocComment());
// string(66) "/**
// * Just a test constant.
// *
// * @var string
// */"
```
### ReflectionClassConstant::getModifiers
```php
<?php
class Foo
{
/**
* Just a test constant.
*
* @var string
*/
public const CONSTANT = 'foo';
}
$foo = new ReflectionClassConstant('Foo', 'CONSTANT');
var_dump($foo->getModifiers()); // int(1)
```
### ReflectionClassConstant::getName
```php
<?php
class Foo
{
/**
* Just a test constant.
*
* @var string
*/
public const CONSTANT = 'foo';
}
$foo = new ReflectionClassConstant('Foo', 'CONSTANT');
var_dump($foo->getName()); // string(8) "CONSTANT"
```
### ReflectionClassConstant::getValue
```php
<?php
class Foo
{
/**
* Just a test constant.
*
* @var string
*/
public const CONSTANT = 'foo';
}
$foo = new ReflectionClassConstant('Foo', 'CONSTANT');
var_dump($foo->getValue()); // string(3) "foo"
```
### ReflectionClassConstant::isPrivate
```php
<?php
class Foo
{
/**
* Just a test constant.
*
* @var string
*/
private const CONSTANT = 'foo';
}
$foo = new ReflectionClassConstant('Foo', 'CONSTANT');
var_dump($foo->isPrivate()); // bool(true)
```
### ReflectionClassConstant::isProtected
```php
<?php
class Foo
{
/**
* Just a test constant.
*
* @var string
*/
protected const CONSTANT = 'foo';
}
$foo = new ReflectionClassConstant('Foo', 'CONSTANT');
var_dump($foo->isProtected()); // bool(true)
```
### ReflectionClassConstant::isPublic
```php
<?php
class Foo
{
/**
* Just a test constant.
*
* @var string
*/
public const CONSTANT = 'foo';
}
$foo = new ReflectionClassConstant('Foo', 'CONSTANT');
var_dump($foo->isPublic()); // bool(true)
```
### ReflectionClassConstant::__toString
```php
<?php
class Foo
{
/**
* Just a test constant.
*
* @var string
*/
public const CONSTANT = 'foo';
}
$foo = new ReflectionClassConstant('Foo', 'CONSTANT');
echo $foo; // Constant [ public string CONSTANT ] { foo }
print $foo; // Constant [ public string CONSTANT ] { foo }
```
## ReflectionZendExtension
### ReflectionZendExtension::__construct
```php
<?php
var_dump(new ReflectionZendExtension('Zend OPcache')); // object(ReflectionZendExtension)#1 (1) { ["name"]=> string(12) "Zend OPcache" }
```
### ReflectionZendExtension::getAuthor
```php
<?php
$foo = new ReflectionZendExtension('Zend OPcache');
var_dump($foo->getAuthor()); // string(17) "Zend Technologies"
```
### ReflectionZendExtension::getCopyright
```php
<?php
$foo = new ReflectionZendExtension('Zend OPcache');
var_dump($foo->getCopyright()); // string(13) "Copyright (c)"
```
### ReflectionZendExtension::getName
```php
<?php
$foo = new ReflectionZendExtension('Zend OPcache');
var_dump($foo->getName()); // string(12) "Zend OPcache"
```
### ReflectionZendExtension::getURL
```php
<?php
$foo = new ReflectionZendExtension('Zend OPcache');
var_dump($foo->getURL()); // string(20) "http://www.zend.com/"
```
### ReflectionZendExtension::getVersion
```php
<?php
$foo = new ReflectionZendExtension('Zend OPcache');
var_dump($foo->getVersion());
```
### ReflectionZendExtension::__toString
```php
<?php
$foo = new ReflectionZendExtension('Zend OPcache');
echo $foo;
print $foo;
```
## ReflectionExtension
### ReflectionExtension::__construct
```php
<?php
var_dump(new ReflectionExtension('Reflection')); // object(ReflectionExtension)#1 (1) { ["name"]=> string(10) "Reflection" }
```
### ReflectionExtension::getClasses
```php
<?php
$foo = new ReflectionExtension('Reflection');
var_dump($foo->getClasses());
```
### ReflectionExtension::getClassNames
```php
<?php
$foo = new ReflectionExtension('Reflection');
var_dump($foo->getClassNames());
```
### ReflectionExtension::getConstants
```php
<?php
$foo = new ReflectionExtension('Reflection');
var_dump($foo->getConstants());
```
### ReflectionExtension::getDependencies
```php
<?php
$foo = new ReflectionExtension('Reflection');
var_dump($foo->getDependencies());
```
### ReflectionExtension::getFunctions
```php
<?php
$foo = new ReflectionExtension('Reflection');
var_dump($foo->getFunctions());
```
### ReflectionExtension::getINIEntries
```php
<?php
$foo = new ReflectionExtension('Reflection');
var_dump($foo->getINIEntries());
```
### ReflectionExtension::getName
```php
<?php
$foo = new ReflectionExtension('Reflection');
var_dump($foo->getName()); // string(10) "Reflection"
```
### ReflectionExtension::getVersion
```php
<?php
$foo = new ReflectionExtension('Reflection');
var_dump($foo->getVersion());
```
### ReflectionExtension::info
```php
<?php
$foo = new ReflectionExtension('Reflection');
$foo->info();
```
### ReflectionExtension::isPersistent
```php
<?php
$foo = new ReflectionExtension('Reflection');
var_dump($foo->isPersistent()); // bool(true)
```
### ReflectionExtension::isTemporary
```php
<?php
$foo = new ReflectionExtension('Reflection');
var_dump($foo->isTemporary()); // bool(false)
```
### ReflectionExtension::__toString
```php
<?php
$foo = new ReflectionExtension('Reflection');
echo $foo;
print $foo;
```
## ReflectionFunction
### ReflectionFunction::__construct
```php
<?php
/**
* Just a test function.
*
* @param void
* @return void
*/
function foo()
{
//
}
/**
* Just a test function.
*
* @param void
* @return void
*/
$foo = function () {
//
};
var_dump(new ReflectionFunction('foo')); // object(ReflectionFunction)#2 (1) { ["name"]=> string(3) "foo" }
var_dump(new ReflectionFunction($foo)); // object(ReflectionFunction)#2 (1) { ["name"]=> string(9) "{closure}" }
```
### ReflectionFunction::getClosure
```php
<?php
/**
* Just a test function.
*
* @param void
* @return void
*/
function foo(): string
{
return 'foo';
}
$foo = new ReflectionFunction('foo');
var_dump(($foo->getClosure())()); // string(3) "foo"
```
### ReflectionFunction::invoke
```php
<?php
/**
* Calculate the sum of two integers.
*
* @param int $a
* @param int $b
* @return int
*/
function foo(int $a, int $b): int
{
return $a + $b;
}
$foo = new ReflectionFunction('foo');
var_dump($foo->invoke(2, 3)); // int(5)
```
### ReflectionFunction::invokeArgs
```php
<?php
/**
* Calculate the sum of all integers.
*
* @param int $number
* @return int
*/
function foo(int ...$number): int
{
$sum = 0;
foreach ($number as $value) {
$sum += $value;
}
return $sum;
}
$foo = new ReflectionFunction('foo');
var_dump($foo->invokeArgs([1, 2, 3])); // int(6)
```
### ReflectionFunction::isDisabled
```php
<?php
/**
* Just a test function.
*
* @param void
* @return void
*/
function foo()
{
//
}
$foo = new ReflectionFunction('foo');
var_dump($foo->isDisabled()); // bool(false)
```
### ReflectionFunction::__toString
```php
<?php
/**
* Just a test function.
*
* @param void
* @return void
*/
function foo()
{
//
}
$foo = new ReflectionFunction('foo');
echo $foo;
print $foo;
```
## ReflectionFunctionAbstract
### ReflectionFunctionAbstract::getClosureScopeClass
```php
<?php
/**
* Just a test function.
*
* @param void
* @return void
*/
function foo()
{
//
}
$foo = new ReflectionFunction('foo');
var_dump($foo->getClosureScopeClass()); // NULL
```
### ReflectionFunctionAbstract::getClosureThis
```php
<?php
/**
* Just a test function.
*
* @param void
* @return void
*/
function foo()
{
//
}
$foo = new ReflectionFunction('foo');
var_dump($foo->getClosureThis()); // NULL
```
### ReflectionFunctionAbstract::getDocComment
```php
<?php
/**
* Just a test function.
*
* @param void
* @return void
*/
function foo()
{
//
}
$foo = new ReflectionFunction('foo');
var_dump($foo->getDocComment());
// string(67) "/**
// * Just a test function.
// *
// * @param void
// * @return void
// */"
```
### ReflectionFunctionAbstract::getEndLine
```php
<?php
/**
* Just a test function.
*
* @param void
* @return void
*/
function foo()
{
//
}
$foo = new ReflectionFunction('foo');
var_dump($foo->getEndLine()); // int(12)
```
### ReflectionFunctionAbstract::getExtension
```php
<?php
/**
* Just a test function.
*
* @param void
* @return void
*/
function foo()
{
//
}
$foo = new ReflectionFunction('foo');
var_dump($foo->getExtension()); // NULL
$foo = new ReflectionFunction('mb_strlen');
var_dump($foo->getExtension()); // object(ReflectionExtension)#1 (1) { ["name"]=> string(8) "mbstring" }
```
### ReflectionFunctionAbstract::getExtensionName
```php
<?php
/**
* Just a test function.
*
* @param void
* @return void
*/
function foo()
{
//
}
$foo = new ReflectionFunction('foo');
var_dump($foo->getExtensionName()); // bool(false)
$foo = new ReflectionFunction('mb_strlen');
var_dump($foo->getExtensionName()); // string(8) "mbstring"
```
### ReflectionFunctionAbstract::getFileName
```php
<?php
/**
* Just a test function.
*
* @param void
* @return void
*/
function foo()
{
//
}
$foo = new ReflectionFunction('foo');
var_dump($foo->getFileName());
```
### ReflectionFunctionAbstract::getName
```php
<?php
namespace Foo;
/**
* Just a test function.
*
* @param void
* @return void
*/
function bar()
{
//
}
$foo = new \ReflectionFunction('var_dump');
var_dump($foo->getName()); // string(8) "var_dump"
$bar = new \ReflectionFunction('Foo\bar');
var_dump($bar->getName()); // string(7) "Foo\bar"
```
### ReflectionFunctionAbstract::getNamespaceName
```php
<?php
namespace Foo;
/**
* Just a test function.
*
* @param void
* @return void
*/
function bar()
{
//
}
$foo = new \ReflectionFunction('var_dump');
var_dump($foo->getNamespaceName()); // string(0) ""
$bar = new \ReflectionFunction('Foo\bar');
var_dump($bar->getNamespaceName()); // string(3) "Foo"
```
### ReflectionFunctionAbstract::getNumberOfParameters
```php
<?php
/**
* Just a test function.
*
* @param void
* @return void
*/
function foo()
{
//
}
$foo = new ReflectionFunction('foo');
var_dump($foo->getNumberOfParameters()); // int(0)
$foo = new ReflectionFunction('var_dump');
var_dump($foo->getNumberOfParameters()); // int(1)
```
### ReflectionFunctionAbstract::getNumberOfRequiredParameters
```php
<?php
/**
* Just a test function.
*
* @param string $string
* @return string
*/
function foo(string $string = 'foo'): string
{
return $string;
}
$foo = new ReflectionFunction('foo');
var_dump($foo->getNumberOfRequiredParameters()); // int(0)
$foo = new ReflectionFunction('var_dump');
var_dump($foo->getNumberOfRequiredParameters()); // int(1)
```
### ReflectionFunctionAbstract::getParameters
```php
<?php
/**
* Just a test function.
*
* @param void
* @return void
*/
function foo()
{
//
}
/**
* Calculate the sum of two integers.
*
* @param int $a
* @param int $b
* @return int
*/
function bar(int $a, int $b): int
{
return $a + $b;
}
$foo = new ReflectionFunction('foo');
var_dump($foo->getParameters()); // array(0) { }
$bar = new ReflectionFunction('bar');
var_dump($bar->getParameters()); // array(2) { [0]=> object(ReflectionParameter)#3 (1) { ["name"]=> string(1) "a" } [1]=> object(ReflectionParameter)#4 (1) { ["name"]=> string(1) "b" } }
```
### ReflectionFunctionAbstract::getReturnType
```php
<?php
/**
* Just a test function.
*
* @param void
* @return void
*/
function foo()
{
//
}
/**
* Just a test function.
*
* @param void
* @return string
*/
function bar(): string
{
return 'bar';
}
$foo = new ReflectionFunction('foo');
var_dump($foo->getReturnType()); // NULL
$bar = new ReflectionFunction('bar');
var_dump($bar->getReturnType()); // object(ReflectionNamedType)#3 (0) { }
```
### ReflectionFunctionAbstract::getShortName
```php
<?php
namespace Foo;
/**
* Just a test function.
*
* @param void
* @return void
*/
function bar()
{
//
}
$foo = new \ReflectionFunction('var_dump');
var_dump($foo->getShortName()); // string(8) "var_dump"
$bar = new \ReflectionFunction('Foo\bar');
var_dump($bar->getShortName()); // string(3) "bar"
```
### ReflectionFunctionAbstract::getStartLine
```php
<?php
/**
* Just a test function.
*
* @param void
* @return void
*/
function foo()
{
//
}
$foo = new ReflectionFunction('foo');
var_dump($foo->getStartLine()); // int(9)
```
### ReflectionFunctionAbstract::getStaticVariables
```php
<?php
/**
* Just a test function.
*
* @param void
* @return void
*/
function foo()
{
static $a = 2;
static $b = 3;
}
$foo = new ReflectionFunction('foo');
var_dump($foo->getStaticVariables()); // array(2) { ["a"]=> int(2) ["b"]=> int(3) }
```
### ReflectionFunctionAbstract::hasReturnType
```php
<?php
/**
* Just a test function.
*
* @param void
* @return void
*/
function foo()
{
//
}
/**
* Just a test function.
*
* @param void
* @return string
*/
function bar(): string
{
return 'bar';
}
$foo = new ReflectionFunction('foo');
var_dump($foo->hasReturnType()); // bool(false)
$bar = new ReflectionFunction('bar');
var_dump($bar->hasReturnType()); // bool(true)
```
### ReflectionFunctionAbstract::inNamespace
```php
<?php
namespace Foo;
/**
* Just a test function.
*
* @param void
* @return void
*/
function bar()
{
//
}
$foo = new \ReflectionFunction('var_dump');
var_dump($foo->inNamespace()); // bool(false)
$bar = new \ReflectionFunction('Foo\bar');
var_dump($bar->inNamespace()); // bool(true)
```
### ReflectionFunctionAbstract::isClosure
```php
<?php
/**
* Just a test function.
*
* @param void
* @return void
*/
function foo()
{
//
}
/**
* Just a test function.
*
* @param void
* @return void
*/
$bar = function () {
//
};
$foo = new ReflectionFunction('foo');
var_dump($foo->isClosure()); // bool(false)
$bar = new ReflectionFunction($bar);
var_dump($bar->isClosure()); // bool(true)
```
### ReflectionFunctionAbstract::isDeprecated
```php
<?php
/**
* Just a test function.
*
* @param void
* @return void
*/
function foo()
{
//
}
$foo = new ReflectionFunction('foo');
var_dump($foo->isDeprecated()); // bool(false)
```
### ReflectionFunctionAbstract::isGenerator
```php
<?php
/**
* Just a test function.
*
* @param void
* @return void
*/
function foo()
{
//
}
/**
* Yielding test value.
*
* @param void
* @return void
*/
function bar()
{
yield 1;
}
$foo = new ReflectionFunction('foo');
var_dump($foo->isGenerator()); // bool(false)
$foo = new ReflectionFunction('bar');
var_dump($foo->isGenerator()); // bool(true)
```
### ReflectionFunctionAbstract::isInternal
```php
<?php
/**
* Just a test function.
*
* @param void
* @return void
*/
function foo()
{
//
}
$foo = new ReflectionFunction('foo');
var_dump($foo->isInternal()); // bool(false)
$foo = new ReflectionFunction('var_dump');
var_dump($foo->isInternal()); // bool(true)
```
### ReflectionFunctionAbstract::isUserDefined
```php
<?php
/**
* Just a test function.
*
* @param void
* @return void
*/
function foo()
{
//
}
$foo = new ReflectionFunction('foo');
var_dump($foo->isUserDefined()); // bool(true)
$foo = new ReflectionFunction('var_dump');
var_dump($foo->isUserDefined()); // bool(false)
```
### ReflectionFunctionAbstract::isVariadic
```php
<?php
/**
* Just a test function.
*
* @param void
* @return void
*/
function foo()
{
//
}
$foo = new ReflectionFunction('foo');
var_dump($foo->isVariadic()); // bool(false)
```
### ReflectionFunctionAbstract::returnsReference
```php
<?php
/**
* Just a test function.
*
* @param void
* @return void
*/
function foo()
{
//
}
/**
* Return a reference.
*
* @param void
* @return int
*/
function &bar(): int
{
static $a = 2;
return $a++;
}
$foo = new ReflectionFunction('foo');
var_dump($foo->returnsReference()); // bool(false)
$bar = new ReflectionFunction('bar');
var_dump($bar->returnsReference()); // bool(true)
```
## ReflectionMethod
### ReflectionMethod::__construct
```php
<?php
class Foo
{
/**
* Just a test method.
*
* @param void
* @return void
*/
public function method()
{
//
}
}
var_dump(new ReflectionMethod('Foo', 'method'));
var_dump(new ReflectionMethod('Foo::method'));
```
### ReflectionMethod::getClosure
```php
<?php
class Foo
{
/**
* Public method.
*
* @param void
* @return string
*/
public function public(): string
{
return 'public';
}
/**
* Protected method.
*
* @param void
* @return string
*/
protected function protected(): string
{
return 'protected';
}
/**
* Private method.
*
* @param void
* @return string
*/
private function private(): string
{
return 'private';
}
}
$foo = new ReflectionMethod('Foo', 'public');
var_dump(($foo->getClosure(new Foo()))()); // string(6) "public"
$foo = new ReflectionMethod('Foo', 'protected');
var_dump(($foo->getClosure(new Foo()))()); // string(9) "protected"
$foo = new ReflectionMethod('Foo', 'private');
var_dump(($foo->getClosure(new Foo()))()); // string(7) "private"
```
### ReflectionMethod::getDeclaringClass
```php
<?php
class Foo
{
/**
* Just a test method.
*
* @param void
* @return void
*/
public function method()
{
//
}
}
$foo = new ReflectionMethod('Foo', 'method');
var_dump($foo->getDeclaringClass()); // object(ReflectionClass)#2 (1) { ["name"]=> string(3) "Foo" }
```
### ReflectionMethod::getModifiers
```php
<?php
class Foo
{
/**
* Public method.
*
* @param void
* @return void
*/
final public function public()
{
//
}
/**
* Protected method.
*
* @param void
* @return void
*/
protected static function protected()
{
//
}
/**
* Private method.
*
* @param void
* @return void
*/
private function private()
{
//
}
}
$foo = new ReflectionMethod('Foo', 'public');
var_dump($foo->getModifiers()); // int(33)
$foo = new ReflectionMethod('Foo', 'protected');
var_dump($foo->getModifiers()); // int(18)
$foo = new ReflectionMethod('Foo', 'private');
var_dump($foo->getModifiers()); // int(4)
```
### ReflectionMethod::getPrototype
```php
<?php
class Foo
{
/**
* Just a test method.
*
* @param void
* @return void
*/
public function method()
{
//
}
}
class Bar extends Foo
{
/**
* Override the parent method.
*
* @param void
* @return string
*/
public function method(): string
{
return 'bar';
}
}
$foo = new ReflectionMethod('Foo', 'method');
var_dump($foo->getPrototype()); // PHP Fatal error: Uncaught ReflectionException: Method Foo::method does not have a prototype.
$bar = new ReflectionMethod('Bar', 'method');
var_dump($bar->getPrototype()); // object(ReflectionMethod)#3 (2) { ["name"]=> string(6) "method" ["class"]=> string(3) "Foo" }
```
### ReflectionMethod::invoke
```php
<?php
class Foo
{
/**
* Calculate the sum of two integers.
*
* @param int $a
* @param int $b
* @return int
*/
public function method(int $a, int $b): int
{
return $a + $b;
}
}
$foo = new ReflectionMethod('Foo', 'method');
var_dump($foo->invoke(new Foo(), 2, 3)); // int(5)
```
### ReflectionMethod::invokeArgs
```php
<?php
class Foo
{
/**
* Calculate the sum of all integers.
*
* @param array $number
* @return int
*/
public function method(array $number): int
{
$sum = 0;
foreach ($number as $value) {
$sum += $value;
}
return $sum;
}
}
$foo = new ReflectionMethod('Foo', 'method');
var_dump($foo->invoke(new Foo(), [1, 2, 3])); // int(6)
```
### ReflectionMethod::isAbstract
```php
<?php
abstract class Foo
{
/**
* Just an abstract method.
*
* @param void
* @return void
*/
abstract public function method();
}
$foo = new ReflectionMethod('Foo', 'method');
var_dump($foo->isAbstract()); // bool(true)
```
### ReflectionMethod::isConstructor
```php
<?php
class Foo
{
/**
* Just a test constructor.
*
* @param void
* @return void
*/
public function __construct()
{
//
}
/**
* Just a test method.
*
* @param void
* @return void
*/
public function method()
{
//
}
}
$foo = new ReflectionMethod('Foo', '__construct');
var_dump($foo->isConstructor()); // bool(true)
$foo = new ReflectionMethod('Foo', 'method');
var_dump($foo->isConstructor()); // bool(false)
```
### ReflectionMethod::isDestructor
```php
<?php
class Foo
{
/**
* Just a test destructor.
*
* @param void
* @return void
*/
public function __destruct()
{
//
}
/**
* Just a test method.
*
* @param void
* @return void
*/
public function method()
{
//
}
}
$foo = new ReflectionMethod('Foo', '__destruct');
var_dump($foo->isDestructor()); // bool(true)
$foo = new ReflectionMethod('Foo', 'method');
var_dump($foo->isDestructor()); // bool(false)
```
### ReflectionMethod::isFinal
```php
<?php
class Foo
{
/**
* Just a test method.
*
* @param void
* @return void
*/
final public function method()
{
//
}
}
$foo = new ReflectionMethod('Foo', 'method');
var_dump($foo->isFinal()); // bool(true)
```
### ReflectionMethod::isPrivate
```php
<?php
class Foo
{
/**
* Just a test method.
*
* @param void
* @return void
*/
private function method()
{
//
}
}
$foo = new ReflectionMethod('Foo', 'method');
var_dump($foo->isPrivate()); // bool(true)
```
### ReflectionMethod::isProtected
```php
<?php
class Foo
{
/**
* Just a test method.
*
* @param void
* @return void
*/
protected function method()
{
//
}
}
$foo = new ReflectionMethod('Foo', 'method');
var_dump($foo->isProtected()); // bool(true)
```
### ReflectionMethod::isPublic
```php
<?php
class Foo
{
/**
* Just a test method.
*
* @param void
* @return void
*/
public function method()
{
//
}
}
$foo = new ReflectionMethod('Foo', 'method');
var_dump($foo->isPublic()); // bool(true)
```
### ReflectionMethod::isStatic
```php
<?php
class Foo
{
/**
* Just a test method.
*
* @param void
* @return void
*/
public static function method()
{
//
}
}
$foo = new ReflectionMethod('Foo', 'method');
var_dump($foo->isStatic()); // bool(true)
```
### ReflectionMethod::setAccessible
```php
<?php
class Foo
{
/**
* Calculate the sum of two integers.
*
* @param int $a
* @param int $b
* @return int
*/
private function method(int $a, int $b): int
{
return $a + $b;
}
}
$foo = new ReflectionMethod('Foo', 'method');
$foo->setAccessible(true);
var_dump($foo->invoke(new Foo(), 2, 3)); // int(5)
```
### ReflectionMethod::__toString
```php
<?php
class Foo
{
/**
* Just a test method.
*
* @param void
* @return void
*/
public function method()
{
//
}
}
$foo = new ReflectionMethod('Foo', 'method');
echo $foo;
print $foo;
```
## ReflectionObject
### ReflectionObject::__construct
```php
<?php
class Foo
{
//
}
var_dump(new ReflectionObject(new Foo())); // object(ReflectionObject)#1 (1) { ["name"]=> string(3) "Foo" }
```
## ReflectionParameter
### ReflectionParameter::__construct
```php
<?php
/**
* Calculate the sum of two integers.
*
* @param int $a
* @param int $b
* @return int
*/
function foo(int $a, int $b): int
{
return $a + $b;
}
var_dump(new ReflectionParameter('foo', 'a')); // object(ReflectionParameter)#1 (1) { ["name"]=> string(1) "a" }
var_dump(new ReflectionParameter('foo', 0)); // object(ReflectionParameter)#1 (1) { ["name"]=> string(1) "a" }
var_dump(new ReflectionParameter('foo', 'b')); // object(ReflectionParameter)#1 (1) { ["name"]=> string(1) "b" }
var_dump(new ReflectionParameter('foo', 1)); // object(ReflectionParameter)#1 (1) { ["name"]=> string(1) "b" }
```
### ReflectionParameter::allowsNull
```php
<?php
/**
* Just a test function.
*
* @param string $string
* @return void
*/
function foo(string $string)
{
//
}
/**
* Just a test function.
*
* @param string|null $string
* @return void
*/
function bar(?string $string = null)
{
//
}
$foo = new ReflectionParameter('foo', 'string');
var_dump($foo->allowsNull()); // bool(false)
$bar = new ReflectionParameter('bar', 'string');
var_dump($bar->allowsNull()); // bool(true)
```
### ReflectionParameter::canBePassedByValue
```php
<?php
/**
* Just a test function.
*
* @param string $string
* @return void
*/
function foo(string $string)
{
//
}
/**
* Just a test function.
*
* @param string &$string
* @return void
*/
function bar(string &$string)
{
//
}
$foo = new ReflectionParameter('foo', 'string');
var_dump($foo->canBePassedByValue()); // bool(true)
$bar = new ReflectionParameter('bar', 'string');
var_dump($bar->canBePassedByValue()); // bool(false)
```
### ReflectionParameter::getClass
```php
<?php
/**
* Just a test function.
*
* @param string $string
* @return void
*/
function foo(string $string)
{
//
}
/**
* Just a test function.
*
* @param Exception $exception
* @return void
*/
function bar(Exception $exception)
{
//
}
$foo = new ReflectionParameter('foo', 'string');
var_dump($foo->getClass()); // NULL
$bar = new ReflectionParameter('bar', 'exception');
var_dump($bar->getClass()); // object(ReflectionClass)#3 (1) { ["name"]=> string(9) "Exception" }
```
### ReflectionParameter::getDeclaringClass
```php
<?php
class Foo
{
/**
* Just a test method.
*
* @param string $string
* @return void
*/
public function method(string $string)
{
//
}
}
$foo = new ReflectionParameter([new Foo(), 'method'], 'string');
var_dump($foo->getDeclaringClass()); // object(ReflectionClass)#2 (1) { ["name"]=> string(3) "Foo" }
```
### ReflectionParameter::getDeclaringFunction
```php
<?php
/**
* Just a test function.
*
* @param string $string
* @return void
*/
function foo(string $string)
{
//
}
$foo = new ReflectionParameter('foo', 'string');
var_dump($foo->getDeclaringFunction()); // object(ReflectionFunction)#2 (1) { ["name"]=> string(3) "foo" }
```
### ReflectionParameter::getDefaultValue
```php
<?php
/**
* Just a test function.
*
* @param string $string
* @return void
*/
function foo(string $string)
{
//
}
/**
* Just a test function.
*
* @param string $string
* @return void
*/
function bar(string $string = 'bar')
{
//
}
$foo = new ReflectionParameter('foo', 'string');
var_dump($foo->getDefaultValue()); // PHP Fatal error: Uncaught ReflectionException: Internal error: Failed to retrieve the default value.
$bar = new ReflectionParameter('bar', 'string');
var_dump($bar->getDefaultValue()); // string(3) "bar"
```
### ReflectionParameter::getDefaultValueConstantName
```php
<?php
/**
* Just a test function.
*
* @param int $number
* @return void
*/
function foo(int $number = PHP_INI_MIN)
{
//
}
$foo = new ReflectionParameter('foo', 'number');
var_dump($foo->getDefaultValueConstantName()); // string(11) "PHP_INI_MIN"
```
### ReflectionParameter::getName
```php
<?php
/**
* Just a test function.
*
* @param string $string
* @return void
*/
function foo(string $string)
{
//
}
$foo = new ReflectionParameter('foo', 'string');
var_dump($foo->getName()); // string(6) "string"
```
### ReflectionParameter::getPosition
```php
<?php
/**
* Calculate the sum of two integers.
*
* @param int $a
* @param int $b
* @return int
*/
function foo(int $a, int $b): int
{
return $a + $b;
}
$foo = new ReflectionParameter('foo', 'a');
var_dump($foo->getPosition()); // int(0)
$foo = new ReflectionParameter('foo', 'b');
var_dump($foo->getPosition()); // int(1)
```
### ReflectionParameter::getType
```php
<?php
/**
* Just a test function.
*
* @param int $number
* @param string $string
* @return void
*/
function foo(int $number, string $string)
{
//
}
$foo = new ReflectionParameter('foo', 'number');
var_dump($foo->getType()); // object(ReflectionNamedType)#2 (0) { }
$foo = new ReflectionParameter('foo', 'string');
var_dump($foo->getType()); // object(ReflectionNamedType)#1 (0) { }
```
### ReflectionParameter::hasType
```php
<?php
/**
* Just a test function.
*
* @param string $string
* @return void
*/
function foo(string $string)
{
//
}
$foo = new ReflectionParameter('foo', 'string');
var_dump($foo->hasType()); // bool(true)
```
### ReflectionParameter::isArray
```php
<?php
/**
* Just a test function.
*
* @param string $string
* @return void
*/
function foo(string $string)
{
//
}
/**
* Just a test function.
*
* @param array $array
* @return void
*/
function bar(array $array)
{
//
}
$foo = new ReflectionParameter('foo', 'string');
var_dump($foo->isArray()); // bool(false)
$bar = new ReflectionParameter('bar', 'array');
var_dump($bar->isArray()); // bool(true)
```
### ReflectionParameter::isCallable
```php
<?php
$foo = new ReflectionParameter('array_map', 0);
var_dump($foo->isCallable()); // bool(false)
```
### ReflectionParameter::isDefaultValueAvailable
```php
<?php
/**
* Just a test function.
*
* @param string $string
* @return void
*/
function foo(string $string)
{
//
}
/**
* Just a test function.
*
* @param string $string
* @return void
*/
function bar(string $string = 'bar')
{
//
}
$foo = new ReflectionParameter('foo', 'string');
var_dump($foo->isDefaultValueAvailable()); // bool(false)
$bar = new ReflectionParameter('bar', 'string');
var_dump($bar->isDefaultValueAvailable()); // bool(true)
```
### ReflectionParameter::isDefaultValueConstant
```php
<?php
/**
* Just a test function.
*
* @param int $number
* @return void
*/
function foo(int $number = PHP_INI_MIN)
{
//
}
$foo = new ReflectionParameter('foo', 'number');
var_dump($foo->isDefaultValueConstant()); // bool(true)
```
### ReflectionParameter::isOptional
```php
<?php
/**
* Just a test function.
*
* @param string $string
* @return void
*/
function foo(string $string)
{
//
}
/**
* Just a test function.
*
* @param string $string
* @return void
*/
function bar(string $string = 'bar')
{
//
}
$foo = new ReflectionParameter('foo', 'string');
var_dump($foo->isOptional()); // bool(false)
$bar = new ReflectionParameter('bar', 'string');
var_dump($bar->isOptional()); // bool(true)
```
### ReflectionParameter::isPassedByReference
```php
<?php
/**
* Just a test function.
*
* @param string $string
* @return void
*/
function foo(string $string)
{
//
}
/**
* Just a test function.
*
* @param string &$string
* @return void
*/
function bar(string &$string)
{
//
}
$foo = new ReflectionParameter('foo', 'string');
var_dump($foo->isPassedByReference()); // bool(false)
$bar = new ReflectionParameter('bar', 'string');
var_dump($bar->isPassedByReference()); // bool(true)
```
### ReflectionParameter::isVariadic
```php
<?php
/**
* Just a test function.
*
* @param string $string
* @return void
*/
function foo(string $string)
{
//
}
$foo = new ReflectionParameter('foo', 'string');
var_dump($foo->isVariadic()); // bool(false)
```
### ReflectionParameter::__toString
```php
<?php
/**
* Just a test function.
*
* @param string $string
* @return void
*/
function foo(string $string)
{
//
}
$foo = new ReflectionParameter('foo', 'string');
echo $foo; // Parameter #0 [ string $string ]
print $foo; // Parameter #0 [ string $string ]
```
## ReflectionProperty
### ReflectionProperty::__construct
```php
<?php
class Foo
{
/**
* Just a test property.
*
* @var string
*/
public string $property = 'foo';
}
var_dump(new ReflectionProperty('Foo', 'property')); // object(ReflectionProperty)#1 (2) { ["name"]=> string(8) "property" ["class"]=> string(3) "Foo" }
```
### ReflectionProperty::getDeclaringClass
```php
<?php
class Foo
{
/**
* Just a test property.
*
* @var string
*/
public string $property = 'foo';
}
$foo = new ReflectionProperty('Foo', 'property');
var_dump($foo->getDeclaringClass()); // object(ReflectionClass)#2 (1) { ["name"]=> string(3) "Foo" }
```
### ReflectionProperty::getDocComment
```php
<?php
class Foo
{
/**
* Just a test property.
*
* @var string
*/
public string $property = 'foo';
}
$foo = new ReflectionProperty('Foo', 'property');
var_dump($foo->getDocComment());
// string(66) "/**
// * Just a test property.
// *
// * @var string
// */"
```
### ReflectionProperty::getModifiers
```php
<?php
class Foo
{
/**
* Public property.
*
* @var string
*/
public static string $public = 'public';
/**
* Protected property.
*
* @var string
*/
protected string $protected = 'protected';
/**
* Private property.
*
* @var string
*/
private string $private = 'private';
}
$foo = new ReflectionProperty('Foo', 'public');
var_dump($foo->getModifiers()); // int(17)
$foo = new ReflectionProperty('Foo', 'protected');
var_dump($foo->getModifiers()); // int(2)
$foo = new ReflectionProperty('Foo', 'private');
var_dump($foo->getModifiers()); // int(4)
```
### ReflectionProperty::getName
```php
<?php
class Foo
{
/**
* Just a test property.
*
* @var string
*/
public string $property = 'foo';
}
$foo = new ReflectionProperty('Foo', 'property');
var_dump($foo->getName()); // string(8) "property"
```
### ReflectionProperty::getType
```php
<?php
class Foo
{
/**
* Just a test property.
*
* @var string
*/
public string $property = 'foo';
}
$foo = new ReflectionProperty('Foo', 'property');
var_dump($foo->getType()); // object(ReflectionNamedType)#2 (0) { }
```
### ReflectionProperty::getValue
```php
<?php
class Foo
{
/**
* Just a test property.
*
* @var string
*/
public string $property = 'foo';
}
$foo = new ReflectionProperty('Foo', 'property');
var_dump($foo->getValue(new Foo())); // string(3) "foo"
```
### ReflectionProperty::hasType
```php
<?php
class Foo
{
/**
* Just a test property.
*
* @var string
*/
public string $foo = 'foo';
/**
* Just a test property.
*
* @var string
*/
public $bar = 'bar';
}
$foo = new ReflectionProperty('Foo', 'foo');
var_dump($foo->hasType()); // bool(true)
$foo = new ReflectionProperty('Foo', 'bar');
var_dump($foo->hasType()); // bool(false)
```
### ReflectionProperty::isDefault
```php
<?php
class Foo
{
/**
* Just a test property.
*
* @var string|null
*/
public ?string $foo;
}
$foo = new ReflectionProperty('Foo', 'foo');
var_dump($foo->isDefault()); // bool(true)
```
### ReflectionProperty::isInitialized
```php
<?php
class Foo
{
/**
* Just a test property.
*
* @var string|null
*/
public ?string $foo;
/**
* Just a test property.
*
* @var string
*/
public string $bar = 'bar';
}
$foo = new ReflectionProperty('Foo', 'foo');
var_dump($foo->isInitialized(new Foo())); // bool(false)
$foo = new ReflectionProperty('Foo', 'bar');
var_dump($foo->isInitialized(new Foo())); // bool(true)
```
### ReflectionProperty::isPrivate
```php
<?php
class Foo
{
/**
* Just a test property.
*
* @var string
*/
private string $property = 'foo';
}
$foo = new ReflectionProperty('Foo', 'property');
var_dump($foo->isPrivate()); // bool(true)
```
### ReflectionProperty::isProtected
```php
<?php
class Foo
{
/**
* Just a test property.
*
* @var string
*/
protected string $property = 'foo';
}
$foo = new ReflectionProperty('Foo', 'property');
var_dump($foo->isProtected()); // bool(true)
```
### ReflectionProperty::isPublic
```php
<?php
class Foo
{
/**
* Just a test property.
*
* @var string
*/
public string $property = 'foo';
}
$foo = new ReflectionProperty('Foo', 'property');
var_dump($foo->isPublic()); // bool(true)
```
### ReflectionProperty::isStatic
```php
<?php
class Foo
{
/**
* Just a test property.
*
* @var string
*/
public static string $property = 'foo';
}
$foo = new ReflectionProperty('Foo', 'property');
var_dump($foo->isStatic()); // bool(true)
```
### ReflectionProperty::setAccessible
```php
<?php
class Foo
{
/**
* Just a test property.
*
* @var string
*/
private string $property = 'foo';
}
$foo = new ReflectionProperty('Foo', 'property');
$foo->setAccessible(true);
var_dump($foo->getValue(new Foo())); // string(3) "foo"
```
### ReflectionProperty::setValue
```php
<?php
class Foo
{
/**
* Just a test property.
*
* @var string
*/
public string $foo = 'foo';
/**
* Just a test property.
*
* @var string
*/
public static string $bar = 'bar';
}
$foo = new Foo();
$reflection = new ReflectionProperty('Foo', 'foo');
$reflection->setValue($foo, 'bar');
var_dump($foo->foo); // string(3) "bar"
$reflection = new ReflectionProperty('Foo', 'bar');
$reflection->setValue('foo');
var_dump(Foo::$bar); // string(3) "foo"
```
### ReflectionProperty::__toString
```php
<?php
class Foo
{
/**
* Just a test property.
*
* @var string
*/
public string $property = 'foo';
}
$foo = new ReflectionProperty('Foo', 'property');
echo $foo; // Property [ <default> public $property ]
print $foo; // Property [ <default> public $property ]
```
## ReflectionType
### ReflectionType::allowsNull
```php
<?php
/**
* Just a test function.
*
* @param string $string
* @return void
*/
function foo(string $string)
{
//
}
$foo = new ReflectionParameter('foo', 'string');
var_dump($foo->allowsNull()); // bool(false)
var_dump($foo->getType()->allowsNull()); // bool(false)
```
### ReflectionType::isBuiltin
```php
<?php
/**
* Just a test function.
*
* @param string $string
* @return void
*/
function foo(string $string)
{
//
}
/**
* Just a test function.
*
* @param Exception $exception
* @return void
*/
function bar(Exception $exception)
{
//
}
$foo = new ReflectionParameter('foo', 'string');
var_dump($foo->getType()->isBuiltin()); // bool(true)
$bar = new ReflectionParameter('bar', 'exception');
var_dump($bar->getType()->isBuiltin()); // bool(false)
```
## ReflectionGenerator
### ReflectionGenerator::__construct
```php
<?php
/**
* Yielding test value.
*
* @param void
* @return void
*/
function foo()
{
yield 1;
}
var_dump(new ReflectionGenerator(foo())); // object(ReflectionGenerator)#1 (0) { }
```
### ReflectionGenerator::getExecutingFile
```php
<?php
/**
* Yielding test value.
*
* @param void
* @return void
*/
function foo()
{
yield 1;
}
$foo = new ReflectionGenerator(foo());
var_dump($foo->getExecutingFile());
```
### ReflectionGenerator::getExecutingGenerator
```php
<?php
/**
* Yielding test value.
*
* @param void
* @return void
*/
function foo()
{
yield 1;
}
$foo = new ReflectionGenerator(foo());
$foo = $foo->getExecutingGenerator();
var_dump($foo); // object(Generator)#2 (0) { }
foreach ($foo as $value) {
var_dump($value);
}
// int(1)
```
### ReflectionGenerator::getExecutingLine
```php
<?php
/**
* Yielding test value.
*
* @param void
* @return void
*/
function foo()
{
yield 1;
}
$foo = new ReflectionGenerator(foo());
var_dump($foo->getExecutingLine()); // int(11)
```
### ReflectionGenerator::getFunction
```php
<?php
/**
* Yielding test value.
*
* @param void
* @return void
*/
function foo()
{
yield 1;
}
$foo = new ReflectionGenerator(foo());
var_dump($foo->getFunction()); // object(ReflectionFunction)#3 (1) { ["name"]=> string(3) "foo" }
```
### ReflectionGenerator::getThis
```php
<?php
class Foo
{
/**
* Yielding test value.
*
* @param void
* @return void
*/
public function method()
{
yield 1;
}
}
$foo = new ReflectionGenerator((new Foo())->method());
var_dump($foo->getThis()); // object(Foo)#2 (0) { }
```
### ReflectionGenerator::getTrace
```php
<?php
/**
* Yielding test value.
*
* @param void
* @return void
*/
function foo()
{
yield 1;
}
/**
* Yielding test value.
*
* @param void
* @return void
*/
function bar()
{
yield from foo();
}
/**
* Yielding test value.
*
* @param void
* @return void
*/
function baz()
{
yield from bar();
}
$baz = baz();
$baz->valid();
$baz = new ReflectionGenerator($baz);
var_dump($baz->getTrace());
```