Download the PHP package zjkiza/default-access without Composer
On this page you can find all versions of the php package zjkiza/default-access. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download zjkiza/default-access
More information about zjkiza/default-access
Files in zjkiza/default-access
Package default-access
Short Description To easily access and invoke private and protected properties and methods as with static.
License MIT
Informations about the package default-access
PHP Package: Default Accessor
A package for easily accessing and invoking private and protected properties and methods, as well as static ones. It also supports access to the methods and properties of the parent class.
About the Package
This package provides a practical way to access and manage private and protected methods and properties in PHP objects through the Zjk\Accessor\Contract\DefaultAccessInterface
.
Methods for Managing Properties and Methods
-
callSetter(object $object, string $method, mixed $value): void
- Invokes a setter method on an object to assign a value.
-
callGetter(object $object, string $method): mixed
- Invokes a getter method on an object to retrieve a value.
-
callMethod(object $object, string $method, array $arguments = []): mixed
- Allows the invocation of any private or protected method, including those with multiple arguments. It also supports the naming of the argument.
- Useful for: Methods that require complex parameters or perform combined operations like setting and retrieving values.
-
setValue(object $object, string $property, mixed $value): void
- Provides direct access to assign a value to a private or protected property.
getValue(object $object, string $property): mixed
- Provides direct access to retrieve the value of a private or protected property.
This tool is ideal for scenarios such as testing, debugging, or working with complex objects where modifying the visibility of methods and properties is not possible or practical.
Installation
Add the package to your composer.json file:
If you're using the Composer autoloader, all necessary files will be automatically included.
Working with the Package
Usage Without a Framework
If you're working in a plain PHP project without a framework, you can directly use the DefaultAccessor::create()
class. This class implements the DefaultAccessInterface and provides access to all its methods.
Usage with a Framework
If you're using a PHP framework, you can integrate this package through dependency injection.
-
Register the Interface and Implementation:
- Configure the
Zjk\Accessor\Contract\DefaultAccessInterface
interface to instantiate theZjk\Accessor\DefaultAccessor
class. - This allows the
DefaultAccessInterface
to be injected into any class within the framework.
- Configure the
- Direct Usage:
- Alternatively, you can use the static
DefaultAccessor::create()
method for instantiation without additional configuration.
- Alternatively, you can use the static
Example in Symfony
-
Service registration in services.yaml:
- Usage in a code:
Example in Laravel
-
Add the binding in a service provider, such as
AppServiceProvider
- Usage in a code:
Package Benefits
- Flexibility: Enables interaction with objects that have private or protected attributes/methods without modifying their visibility.
- Testing Efficiency: Facilitates isolating and validating the behavior of encapsulated objects in unit tests.
- Simplicity: Provides a clean and intuitive API for day-to-day use.
This package is an excellent tool for advanced PHP developers who need a powerful and practical way to access "hidden" parts of objects without compromising object-oriented design principles.