承接 bentools/reflection-plus 相关项目开发

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

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

bentools/reflection-plus

Composer 安装命令:

composer require bentools/reflection-plus

包简介

Another improved code reflection API

README 文档

README

Latest Version on Packagist Total Downloads Tests Coverage

ReflectionPlus is a lightweight wrapper around PHP's native Reflection API that makes working with reflection simpler, more efficient, and more intuitive.

Features

  • Cache-enabled reflection: Automatically caches reflection instances for better performance
  • Simplified API: Clean, intuitive methods for accessing reflection information
  • Type compatibility analysis: Easily determine if classes are compatible with property types
  • Support for complex type systems: Full handling of union types, intersection types, and named types
  • Performance optimized: Uses WeakMap and other optimizations to minimize memory usage

Why?

ReflectionPlus automatically caches:

  • ReflectionClass instances
  • ReflectionProperty instances
  • ReflectionMethod instances
  • Type compatibility results

This makes it highly efficient for repeated usage, especially in loops or recursive operations.

Use Cases

ReflectionPlus is particularly useful for:

  • Factory implementations that need to determine which concrete classes to instantiate
  • Dependency injection containers
  • Type-based serialization/deserialization systems
  • Code generators that need to inspect class structures
  • Data mappers that need to determine type compatibility
  • Framework development where reflection is frequently used

Requirements

  • PHP 8.4 or higher

Usage

Basic Reflection Operations

Get a reflection class:

use BenTools\ReflectionPlus\Reflection;

// From a class name
$reflectionClass = Reflection::class(MyClass::class);

// Or from an object
$object = new MyClass();
$reflectionClass = Reflection::class($object);

Get a reflection property:

// From a class name
$reflectionProperty = Reflection::property(MyClass::class, 'someProperty');

// Or from an object
$object = new MyClass();
$reflectionProperty = Reflection::property($object, 'someProperty');

Get a reflection method:

// From a class name
$reflectionMethod = Reflection::method(MyClass::class, 'someMethod');

// Or from an object
$object = new MyClass();
$reflectionMethod = Reflection::method($object, 'someMethod');

Working with Property Types

Get all instantiable class types that can be set to a property:

// Get a reflection property
$reflectionProperty = Reflection::property($myClass, 'myProperty');

// Get all class types that can be set to this property
$classTypes = Reflection::getSettableClassTypes($reflectionProperty);

// Returns an array of fully qualified class names
// that are compatible with the property type
// and are instantiable (no interfaces or abstract classes)

Check if a class is compatible with a property:

$reflectionProperty = Reflection::property($myClass, 'myProperty');

// Check if a particular class is compatible
$isCompatible = Reflection::isPropertyCompatible($reflectionProperty, SomeClass::class);

// Returns true if SomeClass can be assigned to the property

Find the best class for a property from a list of candidates:

$reflectionProperty = Reflection::property($myClass, 'myProperty');
$classNames = [ClassA::class, ClassB::class, ClassC::class];

// Find the first compatible class in the array
$bestClass = Reflection::getBestClassForProperty($reflectionProperty, $classNames);

// Returns the class name of the first compatible class
// or throws InvalidArgumentException if none are compatible

Type Compatibility

Check type compatibility with different type systems:

$reflectionProperty = Reflection::property($myClass, 'myProperty');
$type = $reflectionProperty->getType();

// Check if a class is compatible with this type
$isCompatible = Reflection::isTypeCompatible($type, SomeClass::class);

// Works with:
// - ReflectionNamedType (standard class or primitive types)
// - ReflectionUnionType (Type1|Type2)
// - ReflectionIntersectionType (Type1&Type2)

Advanced Use Cases

Working with Union Types

// For a property like: public ClassA|ClassB $property;

$reflectionProperty = Reflection::property($class, 'property');
$classTypes = Reflection::getSettableClassTypes($reflectionProperty);

// $classTypes will contain [ClassA::class, ClassB::class] if both are instantiable

Working with Intersection Types

// For a property like: public ClassA&InterfaceB $property;

$reflectionProperty = Reflection::property($class, 'property');

// You can check if a specific class meets all requirements
$isCompatible = Reflection::isPropertyCompatible($reflectionProperty, SomeClass::class);

// Returns true if SomeClass extends/is ClassA AND implements InterfaceB

Installation

You can install the package via composer:

composer require bentools/reflection-plus

License

MIT.

bentools/reflection-plus 适用场景与选型建议

bentools/reflection-plus 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.09k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 05 月 20 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 bentools/reflection-plus 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 bentools/reflection-plus 我们能提供哪些服务?
定制开发 / 二次开发

基于 bentools/reflection-plus 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-05-20