Installation Add the package via Composer:
composer require cetria/reflection-helper
No service provider or package registration is required—it’s a standalone utility.
First Use Case Quickly inspect a class’s properties/methods without manual reflection boilerplate:
use Cetria\ReflectionHelper\ReflectionHelper;
$reflection = ReflectionHelper::getClass('App\Models\User');
$properties = $reflection->getProperties(); // Returns array of property names
$methods = $reflection->getMethods(); // Returns array of method names
Key Entry Points
ReflectionHelper::getClass(string $className): Wraps new ReflectionClass() with convenience methods.ReflectionHelper::getMethod(string $className, string $methodName): Wraps new ReflectionMethod().ReflectionHelper::getProperty(string $className, string $propertyName): Wraps new ReflectionProperty().Dynamic Property/Method Introspection Useful for:
$fields = ReflectionHelper::getClass(User::class)->getProperties();
// Generate Form fields dynamically
$allowed = ['id', 'name'];
$filtered = collect($user)->only(
ReflectionHelper::getClass(User::class)
->getProperties()
->filter(fn($prop) => in_array($prop, $allowed))
);
Dependency Injection & Service Containers Resolve constructor arguments dynamically:
$reflection = ReflectionHelper::getClass(MyService::class);
$params = $reflection->getConstructorParameters();
$container = app();
$service = new MyService(...array_map(fn($param) => $container->make($param), $params));
Laravel-Specific Integrations
$reflection = ReflectionHelper::getClass(User::class);
$protected = $reflection->getProtectedProperties();
// Observe changes to $protected fields
if (ReflectionHelper::getMethod(PostPolicy::class, 'update')->exists()) {
$this->authorize('update', $post);
}
Testing & Mocking Verify method/property existence in unit tests:
public function testUserHasEmailProperty()
{
$this->assertTrue(
ReflectionHelper::getProperty(User::class, 'email')->exists()
);
}
Performance Overhead
$cachedReflection = Cache::remember("reflection_{$className}", now()->addHours(1), fn() =>
ReflectionHelper::getClass($className)
);
Private/Protected Access
ReflectionHelper respects visibility. Force access with:
$property = ReflectionHelper::getProperty(User::class, 'hiddenField');
$property->setAccessible(true);
$value = $property->get($user);
Dynamic Class Loading
ReflectionHelper::getClass($className, true); // $forceLoad = true
Laravel Facade Collisions
Reflection helpers.Chaining Methods Leverage fluent interfaces for cleaner code:
$methods = ReflectionHelper::getClass(User::class)
->getMethods()
->filter(fn($method) => str_starts_with($method, 'set'))
->toArray();
Type Hints & IDE Support
ReflectionClass/ReflectionMethod objects under the hood. Cast to arrays or use native reflection methods:
$method = ReflectionHelper::getMethod(User::class, 'fill');
$params = $method->getParameters(); // Native ReflectionParameterList
Extending Functionality
class CustomReflectionHelper extends ReflectionHelper {
public static function getClass(string $className, bool $includeTraits = true) {
$reflection = parent::getClass($className);
if ($includeTraits) {
$reflection->getTraits(); // Custom logic
}
return $reflection;
}
}
Debugging
dd(
ReflectionHelper::getClass(User::class)
->getProperties()
->mapWithKeys(fn($prop) => [$prop => ReflectionHelper::getProperty(User::class, $prop)->getType()])
);
Laravel Artisan Commands Use reflection to validate command arguments dynamically:
protected function getArguments()
{
$reflection = ReflectionHelper::getClass($this);
return $reflection->getConstructorParameters();
}
How can I help you explore Laravel packages today?