反射

Reflection

Reflection::getModifierNames

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

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

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

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

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

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

/**
 * Just a test class.
 */
class Foo
{
    //
}

$foo = new ReflectionClass('Foo');
var_dump($foo->getDocComment());
// string(29) "/**
//  * Just a test class.
//  */"

ReflectionClass::getEndLine

<?php

class Foo
{
    //
}

$foo = new ReflectionClass('Foo');
var_dump($foo->getEndLine()); // int(6)

ReflectionClass::getExtension

<?php

$foo = new ReflectionClass('ReflectionClass');
var_dump($foo->getExtension()); // object(ReflectionExtension)#2 (1) { ["name"]=> string(10) "Reflection" }

ReflectionClass::getExtensionName

<?php

$foo = new ReflectionClass('ReflectionClass');
var_dump($foo->getExtensionName()); // string(10) "Reflection"

ReflectionClass::getFileName

<?php

class Foo
{
    //
}

$foo = new ReflectionClass('Foo');
var_dump($foo->getFileName());

ReflectionClass::getInterfaceNames

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

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

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

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

class Foo
{
    //
}

$foo = new ReflectionClass('Foo');
var_dump($foo->getModifiers()); // int(0)

ReflectionClass::getName

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

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

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

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

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

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

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

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

class Foo
{
    //
}

$foo = new ReflectionClass('Foo');
var_dump($foo->getStartLine()); // int(3)

ReflectionClass::getStaticProperties

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

class Foo
{
    //
}

$foo = new ReflectionClass('Foo');
echo $foo;
print $foo;

ReflectionClassConstant

ReflectionClassConstant::__construct

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

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

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

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

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

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

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

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

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

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

var_dump(new ReflectionZendExtension('Zend OPcache')); // object(ReflectionZendExtension)#1 (1) { ["name"]=> string(12) "Zend OPcache" }

ReflectionZendExtension::getAuthor

<?php

$foo = new ReflectionZendExtension('Zend OPcache');
var_dump($foo->getAuthor()); // string(17) "Zend Technologies"

ReflectionZendExtension::getCopyright

<?php

$foo = new ReflectionZendExtension('Zend OPcache');
var_dump($foo->getCopyright()); // string(13) "Copyright (c)"

ReflectionZendExtension::getName

<?php

$foo = new ReflectionZendExtension('Zend OPcache');
var_dump($foo->getName()); // string(12) "Zend OPcache"

ReflectionZendExtension::getURL

<?php

$foo = new ReflectionZendExtension('Zend OPcache');
var_dump($foo->getURL()); // string(20) "http://www.zend.com/"

ReflectionZendExtension::getVersion

<?php

$foo = new ReflectionZendExtension('Zend OPcache');
var_dump($foo->getVersion());

ReflectionZendExtension::__toString

<?php

$foo = new ReflectionZendExtension('Zend OPcache');
echo $foo;
print $foo;

ReflectionExtension

ReflectionExtension::__construct

<?php

var_dump(new ReflectionExtension('Reflection')); // object(ReflectionExtension)#1 (1) { ["name"]=> string(10) "Reflection" }

ReflectionExtension::getClasses

<?php

$foo = new ReflectionExtension('Reflection');
var_dump($foo->getClasses());

ReflectionExtension::getClassNames

<?php

$foo = new ReflectionExtension('Reflection');
var_dump($foo->getClassNames());

ReflectionExtension::getConstants

<?php

$foo = new ReflectionExtension('Reflection');
var_dump($foo->getConstants());

ReflectionExtension::getDependencies

<?php

$foo = new ReflectionExtension('Reflection');
var_dump($foo->getDependencies());

ReflectionExtension::getFunctions

<?php

$foo = new ReflectionExtension('Reflection');
var_dump($foo->getFunctions());

ReflectionExtension::getINIEntries

<?php

$foo = new ReflectionExtension('Reflection');
var_dump($foo->getINIEntries());

ReflectionExtension::getName

<?php

$foo = new ReflectionExtension('Reflection');
var_dump($foo->getName()); // string(10) "Reflection"

ReflectionExtension::getVersion

<?php

$foo = new ReflectionExtension('Reflection');
var_dump($foo->getVersion());

ReflectionExtension::info

<?php

$foo = new ReflectionExtension('Reflection');
$foo->info();

ReflectionExtension::isPersistent

<?php

$foo = new ReflectionExtension('Reflection');
var_dump($foo->isPersistent()); // bool(true)

ReflectionExtension::isTemporary

<?php

$foo = new ReflectionExtension('Reflection');
var_dump($foo->isTemporary()); // bool(false)

ReflectionExtension::__toString

<?php

$foo = new ReflectionExtension('Reflection');
echo $foo;
print $foo;

ReflectionFunction

ReflectionFunction::__construct

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

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

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

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

/**
 * Just a test function.
 *
 * @param  void
 * @return void
 */
function foo()
{
    //
}

$foo = new ReflectionFunction('foo');
var_dump($foo->isDisabled()); // bool(false)

ReflectionFunction::__toString

<?php

/**
 * Just a test function.
 *
 * @param  void
 * @return void
 */
function foo()
{
    //
}

$foo = new ReflectionFunction('foo');
echo $foo;
print $foo;

ReflectionFunctionAbstract

ReflectionFunctionAbstract::getClosureScopeClass

<?php

/**
 * Just a test function.
 *
 * @param  void
 * @return void
 */
function foo()
{
    //
}

$foo = new ReflectionFunction('foo');
var_dump($foo->getClosureScopeClass()); // NULL

ReflectionFunctionAbstract::getClosureThis

<?php

/**
 * Just a test function.
 *
 * @param  void
 * @return void
 */
function foo()
{
    //
}

$foo = new ReflectionFunction('foo');
var_dump($foo->getClosureThis()); // NULL

ReflectionFunctionAbstract::getDocComment

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

/**
 * Just a test function.
 *
 * @param  void
 * @return void
 */
function foo()
{
    //
}

$foo = new ReflectionFunction('foo');
var_dump($foo->getEndLine()); // int(12)

ReflectionFunctionAbstract::getExtension

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

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

/**
 * Just a test function.
 *
 * @param  void
 * @return void
 */
function foo()
{
    //
}

$foo = new ReflectionFunction('foo');
var_dump($foo->getFileName());

ReflectionFunctionAbstract::getName

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

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

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

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

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

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

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

/**
 * Just a test function.
 *
 * @param  void
 * @return void
 */
function foo()
{
    //
}

$foo = new ReflectionFunction('foo');
var_dump($foo->getStartLine()); // int(9)

ReflectionFunctionAbstract::getStaticVariables

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

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

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

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

/**
 * Just a test function.
 *
 * @param  void
 * @return void
 */
function foo()
{
    //
}

$foo = new ReflectionFunction('foo');
var_dump($foo->isDeprecated()); // bool(false)

ReflectionFunctionAbstract::isGenerator

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

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

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

/**
 * Just a test function.
 *
 * @param  void
 * @return void
 */
function foo()
{
    //
}

$foo = new ReflectionFunction('foo');
var_dump($foo->isVariadic()); // bool(false)

ReflectionFunctionAbstract::returnsReference

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

class Foo
{
    //
}

var_dump(new ReflectionObject(new Foo())); // object(ReflectionObject)#1 (1) { ["name"]=> string(3) "Foo" }

ReflectionParameter

ReflectionParameter::__construct

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

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

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

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

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

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

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

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

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

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

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

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

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

$foo = new ReflectionParameter('array_map', 0);
var_dump($foo->isCallable()); // bool(false)

ReflectionParameter::isDefaultValueAvailable

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

/**
 * Yielding test value.
 *
 * @param  void
 * @return void
 */
function foo()
{
    yield 1;
}

var_dump(new ReflectionGenerator(foo())); // object(ReflectionGenerator)#1 (0) { }

ReflectionGenerator::getExecutingFile

<?php

/**
 * Yielding test value.
 *
 * @param  void
 * @return void
 */
function foo()
{
    yield 1;
}

$foo = new ReflectionGenerator(foo());
var_dump($foo->getExecutingFile());

ReflectionGenerator::getExecutingGenerator

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

/**
 * Yielding test value.
 *
 * @param  void
 * @return void
 */
function foo()
{
    yield 1;
}

$foo = new ReflectionGenerator(foo());
var_dump($foo->getExecutingLine()); // int(11)

ReflectionGenerator::getFunction

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

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

/**
 * 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());

results matching ""

    No results matching ""