stratadox / instantiator
Requires
- php: >=7.2
Requires (Dev)
- php-coveralls/php-coveralls: ^2.0
- phpstan/phpstan: ^0.9.2
- phpunit/phpunit: ^7.0
This package is auto-updated.
Last update: 2024-10-19 23:23:56 UTC
README
Lightweight instantiator.
Installation
Install using composer require stratadox/instantiator
What is this?
The Instantiator
module provides a most simplistic way of producing empty
instances.
An Instantiator
is configured to produce instances of a specific class.
In this way it differs from most other instantiator packages, which usually
specify the class to instantiate as method parameter.
Basic usage
<?php use Stratadox\Instantiator\ObjectInstantiator; $provideFoo = ObjectInstantiator::forThe(Foo::class); assert($provideFoo->instance() instanceof Foo); assert(Foo::class === $provideFoo->class());
How does it work?
The ObjectInstantiator
class basically just extends ReflectionClass
in order
to alias its newInstanceWithoutConstructor
method.
In cases where this instantiation method fails, for instance when a final class
inherits from an internal class, deserialization is used instead.
Alternatively, this module includes a PredefinedInstanceProvider
. This one
isn't really an instantiator, but rather an iterator, but that does not stop it
from implementing the Instantiator
interface.