1. Go to this page and download the library: Download tholabs/creator 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/ */
tholabs / creator example snippets
$creator = new \Creator\Creator;
$myInstance = $creator->create(MyClass::class);
class MyClass {
function __construct(AnotherClass $anotherClass) {
$this->anotherClass = $anotherClass;
}
}
$container = new Creator\Container(new Creator\Creator());
$myInstance = $container->get(MyClass::class);
$anotherClass = new AnotherClass();
$creator = new Creator\Creator;
$myClass = $creator->createInjected(MyClass::class)->with($anotherClass)->create();
if ($myClass->anotherClass === $anotherClass) {
echo 'We are the same!';
}
// Default invocation
$creator->invoke(function(SimpleClass $simpleClass) {
if ($simpleClass instanceof SimpleClass) {
echo 'Everything works as expected.';
}
}
// Injected invocation
$simpleClassRoleModel = new SimpleClass();
$creator->invokeInjected(function(SimpleClass $simpleClass) use($simpleClassRoleModel) {
if ($simpleClass === $simpleClassRoleModel) {
echo 'Injection is great';
}
})->with($simpleClassRoleModel)->invoke();
$simpleClass = new SimpleClass();
$factory = function() use ($simpleClass) {
return new ExtendedClass($simpleClass);
};
$creator->registerFactory($factory, ExtendedClass::class);
$extendedClass = $creator->create(ExtendedClass::class);
if ($extendedClass->getSimpleClass() === $simpleClass) {
echo 'Factories are awesome!';
}
$simpleClass = new SimpleClass();
$factory = function() use ($simpleClass) {
return new ExtendedClass($simpleClass);
};
$extendedClass = $creator->createInjected(ExtendedClass:class)
->withFactory($factory, ExtendedClass::class)
->create();
if ($extendedClass->getSimpleClass() === $simpleClass) {
echo 'Factories are awesome!';
}
class MyDependency implements Creator\Interfaces\SelfFactory {
static function createSelf () : callable {
return function(AnotherDependency $a) : MyDependency {
return new static($a->getFoo());
}
}
function __construct (Foo $foo) {
$this->foo = $foo;
}
}
$creator->registerFactory(SimpleFactory::class, SimpleClass::class);
// Creates a SimpleClass with SimpleFactory
$simpleClass = $creator->create(SimpleClass::class);
class SimpleFactory implements Factory {
function __construct(SimpleClass $simpleClass) {
$this->simpleClass = $simpleClass;
}
function createInstance() {
return $this->simpleClass;
}
}
$creator->registerFactory(SimpleFactory::class, SimpleClass::class);
$simpleClass = new SimpleClass();
$simpleFactory = new SimpleFactory($simpleClass);
$creator->registerClassResource($simpleFactory);
$generatedSimpleClass = $creator->create(SimpleClass::class);
if ($simpleClass === $generatedSimpleClass) {
echo 'Congratulations, this example is completely useless and works!';
}
$a = new stdClass;
$a->foo = 'bar';
$creator = new Creator\Creator;
$creator->registerClassResource($a);
$instance = $creator->create('stdClass'); // you should not use hardcoded strings as class names; always prefer the class constant
echo $instance->foo; // bar
class A {
function __construct($foo) {
echo $foo;
}
}
$creator = new Creator\Creator;
$creator->createInjected(A::class)
->with('bar', 'foo') // first value is the injection, second the resource key
->create();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.