承接 web-fu/reflection 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

web-fu/reflection

最新稳定版本:v2.5.0

Composer 安装命令:

composer require web-fu/reflection

包简介

Reflection API

关键字:

README 文档

README

Latest Stable Version PHP Version Require Test status Static analysis status Code style status

This library is a type safe wrapper for PHP Reflection API.

This library is born with the purpose to solve the problem of type safety in PHP Reflection API. Reflection API is a very powerful tool, but presents some issues.

For example:

  • the original ReflectionClass::getConstant return false if the constant does not exist or if the constant is equal to false.
  • ReflectionClass::newInstance return a generic object, but it is possible to know the type of the object.
  • New interfaces are added to the Reflection API in different PHP versions, so it is not possible to use them in a cross-version way.

Installation

web-fu/reflection is available on Packagist and can be installed using Composer.

composer require web-fu/reflection

Requires PHP 8.0 or newer.

Usage

This wrapper try to use the same names of the original Reflection API, but with a different namespace.

<?php

require_once __DIR__ . '/vendor/autoload.php';

use WebFu\Reflection\ReflectionClass;
use MyNamespace\MyClass;

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

Type management

PHP Reflection API use different classes to manage types: ReflectionType, ReflectionNamedType and ReflectionUnionType.

I created a single class to manage all types: WebFu\Reflection\ReflectionType. I added a helper function to infer the PHPDocType, if specified

<?php

require_once __DIR__ . '/vendor/autoload.php';

use WebFu\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']

统计信息

  • 总下载量: 5.65k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-03-07