PHP code example of hjerichen / class-instantiator
1. Go to this page and download the library: Download hjerichen/class-instantiator library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
hjerichen / class-instantiator example snippets
use HJerichen\ClassInstantiator\ClassInstantiator;
class ClassA {
}
class ClassB {
public function __construct(ClassA $instanceA) {}
}
$instantiator = new ClassInstantiator();
$object = $instantiator->instantiateClass(ClassB::class);
use HJerichen\ClassInstantiator\ClassInstantiator;
class ClassA {
public function __construct(PDO $database, int $id) {}
}
class ClassB {
public function __construct(ClassA $instanceA) {}
}
$predefinedArguments = [
'database' => new PDO('dsn'),
'id' => 5
];
$instantiator = new ClassInstantiator();
$object = $instantiator->instantiateClass(ClassB::class, $predefinedArguments);
use HJerichen\ClassInstantiator\ClassInstantiator;
class ClassA {
public function __construct(PDO $database, int $id) {}
}
class MyInstantiator extends ClassInstantiator {
# The class instantiator will match the property name to the constructor parameter name
private $id = 5;
# The class instantiator will match the return type to the constructor parameter type
public function getDatabase(): PDO
{
return new PDO('dsn');
}
}
$instantiator = new MyInstantiator();
$object = $instantiator->instantiateClass(ClassA::class);
use HJerichen\ClassInstantiator\Attribute\Instantiator;
use HJerichen\ClassInstantiator\ClassInstantiator;
#[Instantiator(class: MyInstantiator::class)]
class ClassA {
public function __construct(PDO $database, int $id) {}
}
class ClassB {
public function __construct(ClassA $a) {}
}
class MyInstantiator extends ClassInstantiator {
# The class instantiator will match the property name to the constructor parameter name
private $id = 5;
# The class instantiator will match the return type to the constructor parameter type
public function getDatabase(): PDO
{
return new PDO('dsn');
}
}
$instantiator = new ClassInstantiator();
$object = $instantiator->instantiateClass(ClassB::class);
use HJerichen\ClassInstantiator\Attribute\Instantiator;
use HJerichen\ClassInstantiator\ClassInstantiator;
#[Instantiator(class: MyInstantiator::class)]
class ClassA {
public function __construct(PDO $database, int $id) {}
}
class ClassB {
public function __construct(ClassA $a) {}
}
class MyInstantiator extends ClassInstantiator {
# The class instantiator will match the return type to the class type
public function buildClassA(): ClassA
{
$database = new PDO('dsn');
return new ClassA($database, 55);
}
}
$instantiator = new ClassInstantiator();
$object = $instantiator->instantiateClass(ClassB::class);
use HJerichen\ClassInstantiator\Attribute\Instantiator;
use HJerichen\ClassInstantiator\ClassInstantiator;
class ClassA {
public function __construct(PDO $database, int $id) {}
}
class ClassB {
public function __construct(
#[Instantiator(class: MyInstantiator::class)] ClassA $a
) {
}
}
class MyInstantiator extends ClassInstantiator {
# The class instantiator will match the return type to the class type
public function buildClassA(): ClassA
{
$database = new PDO('dsn');
return new ClassA($database, 55);
}
}
$instantiator = new ClassInstantiator();
$object = $instantiator->instantiateClass(ClassB::class);
use HJerichen\ClassInstantiator\Attribute\Instantiator;
use HJerichen\ClassInstantiator\ClassInstantiator;
class ClassA {
public function __construct(PDO $database, int $id) {}
}
class ClassB {
public function __construct(
#[Instantiator(class: MyInstantiator::class, method: 'someMethod')] ClassA $a
) {
}
}
class MyInstantiator extends ClassInstantiator {
# The class instantiator will not use this method to build a ClassA insance.
public function buildClassA(): ClassA
{
$database = new PDO('dsn');
return new ClassA($database, 55);
}
# Is uses this method, because is is specified in the attribute.
public function someMethod(): ClassA
{
$database = new PDO('dsn');
return new ClassA($database, 60);
}
}
$instantiator = new ClassInstantiator();
$object = $instantiator->instantiateClass(ClassB::class);
use HJerichen\ClassInstantiator\Attribute\Instantiator;
#[Instantiator(class: MyInstantiator::class, store: true)]
class ClassA {
public function __construct(PDO $database, int $id) {}
}
use HJerichen\ClassInstantiator\ClassInstantiator;
interface InterfaceA {}
class ClassA implements InterfaceA {}
$instantiator = new ClassInstantiator();
# only using the object resolves to this object when exactly this class is requested
$instantiator->injectObject(new ClassA());
$object = $instantiator->instantiateClass(ClassA::class);
# using the object and an interface as second parameter
# resolves to this object when exactly this interface is requested
$instantiator->injectObject(new ClassA(), InterfaceA::class);
$object = $instantiator->instantiateClass(InterfaceA::class);
use HJerichen\ClassInstantiator\ClassInstantiator;
use HJerichen\ClassInstantiator\ClassInstantiatorContainer;
use TheCodingMachine\GraphQLite\SchemaFactory;
# you can also inject an extension.
$container = new ClassInstantiatorContainer(new ClassInstantiator());
$factory = new SchemaFactory($cache, $container);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.