FireDI – API Documentation

UA1Labs\Fire\Di

get($classname)

Attempts to retrieve an instance object of the given classname by resolving its dependencies and creating an instance of the object.

  • @param $classname string The class you would like to instantiate
  • @throws \UA1Labs\Fire\Di\NotFoundException If the class cannot be resolved
  • @return object The instantiated object based on the $classname
<?php
$fireDi = new UA1Labs\Fire\Di();
$classA = $fireDi->get('ClassA');

set($classname, $entry)

Puts an object into the object cache that is used to resolve dependencies.

  • @param string $classname The classname the instance object should resolve for
  • @param object|callable $entry The object or callable you’d like to place in the object cache
  • @return void
<?php
$fireDi = new UA1Labs\Fire\Di();
$testMock = new Test\Mock();
$fireDi->set('UA1Labs\MyClass', $testMock);

$fireDi->set('MyCallable', function() {
    return new MyClass('awesome');
});

has($classname)

Determines if the class can be resolved.

  • @param string $classname The classname of the instance you would like to resolve
  • @return boolean

getWith($classname, $dependencies)

Returns an instance object for the given classname and dependencies. You may want to use this over Di::get() if you have a need to resolve your own dependencies. This method for getting instance objects also skips the object caching mechanism in FireDi so you will always get a new instance of the object.

  • @param string $classname The classname you want the instance object of
  • @param array<mixed> $dependencies The dependencies that the class is expecting
  • @return object The instance object of the class you asked for
<?php
$fireDi = new UA1Labs\Fire\Di();
$dependencies = [];
$dependencies[] = 'Toyota';
$dependencies[] = 'Corolla';
$toyotaCorolla = $fireDi->getWith('Framework\Model\Car', $dependencies);

clearObjectCache()

Used to clear the cache of any objects that have been instantiated. FireDI maintains an object cache for the purpose of efficiency. Whenever you use Di::get(), the object you obtained, and all of its dependencies are maintained within the object cache. If you ask for the object again, you will retrieve the one that has already been instantiated.

  • @return void
<?php
$fireDi = new UA1Labs\Fire\Di();
$fireDi->clearObjectCache();

Leave a comment

Leave a Reply