PHP code example of web-fu / reflection

1. Go to this page and download the library: Download web-fu/reflection library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

web-fu / reflection example snippets




ebFu\Reflection\ReflectionClass;
use MyNamespace\MyClass;

$reflection = new ReflectionClass(MyClass::class);
echo $reflection->getName(); // MyNamespace\MyClass
echo $reflection->getShortName(); // MyClass



ebFu\Reflection\ReflectionClass;

class ClassWithTypes
{
    public int $simple;
    public int|string $union;
    public $noType;
    public ?int $nullable;
    /** @var class-string */
    public string $className;
}

$reflection = new ReflectionClass(MyClass::class);
echo $reflection->getProperty('simple')->getType()->getTypeNames();         // ['int']
echo $reflection->getProperty('union')->getType()->getTypeNames();          // ['int','string']
echo $reflection->getProperty('noType')->getType()->getTypeNames();         // ['mixed']
echo $reflection->getProperty('nullable')->getType()->getTypeNames();       // ['int','null']
echo $reflection->getProperty('nullable')->getType()->getPhpDocTypeNames(); // ['class-string']