nyholm/nsa
Access non-public properties and methods in PHP for testing and debugging. Nyholm/nsa provides a tiny helper to bypass visibility restrictions (private/protected) without changing production code, making it easy to inspect internals and call hidden methods safely.
nyholm/nsa is a lightweight testing utility focused on simplifying test code by providing quick, no-boilerplate access to PHP's internal state—particularly reflection, closures, and private/protected members—without relying on reflection manually. To start:
composer require --dev nyholm/nsaNsa class directly: use Nsa\Nsa;$user = new User('Alice');
$name = Nsa::getProperty($user, 'name'); // returns 'Alice' even if private
$this->assertEquals('Alice', $name);
Nsa::getProperty() and Nsa::setProperty()—ideal for testing internal state without changing it to public.Nsa::getMethod($object, 'methodName')->invoke($object, ...).Nsa::getClosure($object, 'methodName') to get a reusable closure for repeated calls (e.g., in data providers).Nsa::getNamespace() and Nsa::getTraits() help introspect classes dynamically.Nsa::getMethod() in loops—cache the method closure instead:
$method = Nsa::getMethod($service, 'compute');
$result = $method->invoke($service, $input);
ReflectionMethod, ReflectionProperty, and closures under the hood.nyholm/nsa is listed under require-dev in composer.json.Roave\BetterReflection for advanced cases).$method->invoke(null) fails. Always use Nsa::getMethod($object, 'foo') → $method->invoke($object, ...).Nsa::invoke($obj, 'method', ...) to streamline common patterns.How can I help you explore Laravel packages today?