PHP Classes

PHP Auto Dependency Injection: Create objects classes and dependent classes

Recommend this page to a friend!
  Info   View files Example   View files View files (2)   DownloadInstall with Composer Download .zip   Reputation   Support forum (1)   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStar 57%Total: 136 All time: 9,239 This week: 318Up
Version License PHP version Categories
auto-di 1.0BSD License5.5PHP 5, Language


This class can create objects classes and dependent classes.

It can create a new instance of a given class or return a previously created instance of that class.

The class uses reflection to determine what are the dependent classes by looking at the type hinting classes passed as class function parameters.

The class can also automatically invoke a given function of a class passing instances of the respective classes specified in the type hinting.

Innovation Award
PHP Programming Innovation award nominee
March 2016
Number 16

Prize: One year server license IP to country, region, city, latitude, longitude, ZIP code, time zone, area code database
Nowadays, with type hinting it is possible to determine the types of values that a class expects on each call.

This class can automatically create objects of classes and any dependencies using Reflection to determine what the classes expect when calling its function.

Manuel Lemos
Picture of Aleksandar Zivanovic
  Performance   Level  
Name: Aleksandar Zivanovic <contact>
Classes: 16 packages by
Country: Serbia Serbia
Age: 30
All time rank: 16287 in Serbia Serbia
Week rank: 91 Up3 in Serbia Serbia Up
Innovation award
Innovation award
Nominee: 4x



class ExampleA



    public function
__construct(ExampleA $exampleA)
$this->exampleA = $exampleA;

$this->exampleA->variable = uniqid();

    public function
methodWithDependencies(ExampleA $a, ExampleB $b, $customInteger, $customString)
var_dump($a->variable, $b->uniqueId, $customInteger, $customString);

// Creates new instance of ExampleB and returns it
$exampleBOne = AutoDi::getInstance(ExampleB::class);

// Gets already created instance of ExampleB (Singleton)
$exampleBTwo = AutoDi::getInstance(ExampleB::class);

// Creates new instance of ExampleB without affecting previous
$exampleBThree = AutoDi::getNewInstance(ExampleB::class);

// Set value in $exampleBOne
$exampleBOne->uniqueId = 'this value SHOULD NOT be printed out!!!';

// Set value in $exampleBThree
$exampleBThree->uniqueId = 'this value is for itself';

// Set value in $exampleBTwo
$exampleBTwo->uniqueId = 'this value SHOULD be printed out!!!';

// Executes method "methodWithDependencies" from object (ExampleC)
// last argument are parameters of method that are scalar type or non typed
// e.g:
// methodWithDependencies(ExampleA $a, ExampleB $b, $customInteger, $customString)
// AutoDi will inject first and second parameter (ExampleA and ExampleB) and use
// values from array that is last argument of AutoDi::autoInvoke
// if method looked something like this
// methodWithDependencies(ExampleA $a, $customInteger, ExampleB $b, $customString)
// AutoDi will map non typed parameters with array elements, so $customInteger and $customString
// will be same as in previous example because of order
AutoDi::autoInvoke(AutoDi::getInstance(ExampleC::class), 'methodWithDependencies', [1, 'string']);

  Files folder image Files  
File Role Description
Plain text file AutoDi.php Class Class itself
Accessible without login Plain text file example.php Example Example script

 Version Control Unique User Downloads Download Rankings  
This week:0
All time:9,239
This week:318Up
 User Ratings  
 All time