反射API(一)

2021-06-17 12:05

阅读:484

标签:php   comment   lin   art   fun   etc   exp   结束   ring   


function classData(ReflectionClass $class)
{
    echo ‘
‘;
    $details = ‘当前文件:‘;
    $details .= $class->getFilename().PHP_EOL;
    $details .= ‘类名:‘.$class->getName().PHP_EOL;
    $details .= ‘起始行:‘.$class->getStartLine().PHP_EOL;
    $details .= ‘结束行:‘.$class->getEndLine().PHP_EOL;
    if ($class->isUserDefined()) {
        $details .= ‘由用户定义‘.PHP_EOL;
    }
    if ($class->isInternal()) {
        $details .= ‘由扩展或核心在内部定义‘.PHP_EOL;
    }
    if ($class->isInterface()) {
        $details .= ‘是一个接口‘.PHP_EOL;
    }
    if ($class->isAbstract()) {
        $details .= ‘是final类‘.PHP_EOL;
    }
    if ($class->isFinal()) {
        $details .= ‘是抽象类‘.PHP_EOL;
    }
    if ($class->isInstantiable()) {
        $details .= ‘可实例化‘.PHP_EOL;
    } else {
        $details .= ‘不能实例化‘.PHP_EOL;
    }
    return $details;
}

function methodData(ReflectionMethod $method)
{
    echo ‘
‘;
    $details = ‘当前文件:‘;
    $details .= $method->getFilename().PHP_EOL;
    $details .= ‘方法名:‘.$method->getName().PHP_EOL;
    $details .= ‘起始行:‘.$method->getStartLine().PHP_EOL;
    $details .= ‘结束行:‘.$method->getEndLine().PHP_EOL;
    if ($method->isUserDefined()) {
        $details .= ‘用户定义‘.PHP_EOL;
    }
    if ($method->isInternal()) {
        $details .= ‘是内置函数‘.PHP_EOL;
    }
    if ($method->isPublic()) {
        $details .= ‘是公开方法‘.PHP_EOL;
    }
    if ($method->isProtected()) {
        $details .= ‘是保护方法‘.PHP_EOL;
    }
    if ($method->isPrivate()) {
        $details .= ‘是私有方法‘.PHP_EOL;
    }
    if ($method->isStatic()) {
        $details .= ‘是静态方法‘.PHP_EOL;
    }
    if ($method->isFinal()) {
        $details .= ‘是定义final‘.PHP_EOL;
    }
    if ($method->isConstructor()) {
        $details .= ‘是构造方法‘.PHP_EOL;
    }
    if ($method->returnsReference()) {
        // 只有在方法前加 & 才返回true
        $details .= ‘返回参考信息‘.PHP_EOL;
    }
    // var_dump($method->getDocComment());

    return $details;
}

function argData(ReflectionParameter $arg)
{
    echo ‘
‘;
    $details = ‘‘;
    $declaringclass = $arg->getDeclaringClass();
    $name = $arg->getName();
    $class = $arg->getClass();
    $position = $arg->getPosition();
    $details .= $name.‘是‘.$position.‘个参数‘.PHP_EOL;
    if (!empty($class)) {
        $classname = $class->getName();
        $details .= $name.‘必须是‘.$classname.‘的对象‘.PHP_EOL;
    }
    if ($arg->isPassedByReference()) {
        $details .= $name.‘是引用传值‘.PHP_EOL;
    }
    if ($arg->isDefaultValueAvailable()) {
        $details .= $name.‘有默认值‘.PHP_EOL;
    }

    return $details;
}

class A
{

}

class Product
{
public $name;
protected $price;
private $obj;

public function __construct(&$name, $price=5.99, A $a=null)
{
    $this->name = $name;
    $this->price = $price;
}

/**
* [say description]
* @return [type] [description]
*/
public function say()
{
    echo ‘name:‘.$this->name.‘ price:‘.$this.price;
}
}

echo ‘
‘;
$obj = new ReflectionClass(‘Product‘);
echo classData($obj);

foreach ($obj->getMethods() as $key => $value) {
    echo methodData($value);
}

foreach ($obj->getMethod(‘__construct‘)->getParameters() as $key => $value) {
    echo argData($value);
}

// var_dump(Reflection::export($obj));
// var_dump(ReflectionClass::export(‘Product‘));
// var_dump(ReflectionMethod::export(‘Product‘, ‘say‘));

反射API(一)

标签:php   comment   lin   art   fun   etc   exp   结束   ring   

原文地址:https://www.cnblogs.com/cshaptx4869/p/10332282.html


评论


亲,登录后才可以留言!