roave/better-reflection
Better Reflection is an enhanced PHP reflection API for static analysis. Reflect on classes without loading them, from PHP code strings, and on closures; extract method/function AST and type declarations. Feature-rich but slower than native reflection (not for runtime).
php artisan optimize).composer require roave/better-reflection. No Laravel-specific dependencies or conflicts.Reflection* API, enabling drop-in replacements for core reflection methods (e.g., reflectClass(), reflectMethod()) with extended functionality.Cache facade) or restrict usage to specific contexts (e.g., app:debug commands).memory_get_usage() in CI.BetterReflection::createFromIde() for IDE-specific optimizations or limit scope (e.g., reflect only app/ directory).^6.0) and monitor Laravel’s PHP version roadmap.ReflectionEnumCase), but some edge cases (e.g., dynamic eval()-generated code) may require workarounds.ReflectionProperty for interface methods in traits).app/, config/) or entire vendor space?composer update)?Illuminate\Support\Reflector (a wrapper around native reflection) adequate for the use case?php artisan analyze:types).get_class() hacks with robust reflection in phpunit tests.class_exists() + new ReflectionClass() with BetterReflection for deeper analysis.Booted event to validate service container bindings).Cache facade:
$cacheKey = 'reflection:'.md5($className);
return Cache::remember($cacheKey, now()->addHours(1), fn() =>
(new BetterReflection())->reflector()->reflectClass($className)
);
Reflection*, so most code can be swapped with minimal changes.BetterReflection::reflectFromIde().ShouldQueue.phpunit tests to assert class/method properties (e.g., verify traits are used correctly).6.x → 6.y) are safe. Major updates (e.g., 6.x → 7.x) require testing due to PHP version dependencies.^6.0 and monitor Laravel’s PHP version support.nikic/php-parser (for AST). Monitor for updates to avoid compatibility issues.composer validate step to catch dependency conflicts.memory_get_usage() and microtime().BetterReflection::createFromIde() for IDE-specific optimizations.BetterReflection\Reflection\Exception\IdentifierNotFoundException for graceful error handling.#support on Laravel Discord for reflection-related questions.php artisan).php artisan schedule:run).$start = microtime(true);
$reflection = (new BetterReflection())->reflector()->reflectClass(MyClass::class);
$time = microtime(true) - $start;
Log::info("BetterReflection time: {$time}s");
How can I help you explore Laravel packages today?