PHP Classes

PHP Dependency Injection Container: Autoload dependent objects accessing variables

Recommend this page to a friend!
  Info   View files Example   View files View files (10)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog (2)    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 370 This week: 1All time: 6,807 This week: 560Up
Version License PHP version Categories
pdi 1.0GNU General Publi...7.0Design Patterns, PHP 7
Description 

Author

This class can autoload dependent objects accessing variables.

It provides a dependency injection base class that when it is accessed a given inexistent variable it will automatically load a class with a name based on the variable name and assign it an object of that class.

The class can use a given autoloader to load the dependency class in case it is not yet loaded.

Innovation Award
PHP Programming Innovation award nominee
October 2015
Number 6


Prize: SourceGuarding PHP encoder tool
Dependency injection is a design pattern that allows to configure the behavior of an object using external objects.

This package implements a dependency injection container that autoloads external dependency objects dynamically when certain variables of the dependent object are accessed for the first time.

Manuel Lemos
Picture of Samuel Adeshina
  Performance   Level  
Name: Samuel Adeshina is available for providing paid consulting. Contact Samuel Adeshina .
Classes: 19 packages by
Country: Nigeria Nigeria
Age: 25
All time rank: 5252 in Nigeria Nigeria
Week rank: 106 Up6 in Nigeria Nigeria Down
Innovation award
Innovation award
Nominee: 7x

Winner: 1x

Example

<?php
   
   
require_once("Depender.php");

   
/*
    * Instantiate the Depender Class like a pro ;) [No need to worry about any dependencies]
    */
   
$dependerClass = new Example\Depender(); //
    /*
    * Use a method that makes use of properties/attributes from other classes (Dependee class in this case)
    * without passing those objects (or classes) as parameter!
    */
   
$dependerClass->callDependee(); //isn't this interesting?
?>


Details

CONFUSED?? Don't know which of Injector.php or Injector_5.php to choose from? Injector.php has complete support for PHP 7, it's possible you've not heard of PHP 7 yet or you haven't upgraded (which BTW, you must have a very good reason for) So the script has been rewritten (Injector_5.php) with support for the already 'legacy' versions of php. I recommend leaving the import.php script as is, if you are in a development environment, but please try as much as possible to change exit() to some proper logic you use for error reporting in your production environment. Enjoy! Samuel Adeshina

  Files folder image Files  
File Role Description
Files folder imageexample (4 files)
Files folder imagesrc (4 files)
Accessible without login Plain text file composer.json Data Required Dependencies
Accessible without login Plain text file README.md Data Read Me

  Files folder image Files  /  example  
File Role Description
  Plain text file Dependee.php Class [Example]Dependee Class
  Plain text file Depender.php Class [Example]Depender Class
  Accessible without login Plain text file index.php Example [Example] Index
  Accessible without login Plain text file test.php Example run this file without an autoloader

  Files folder image Files  /  src  
File Role Description
  Accessible without login Plain text file import.php Aux. Use in development only
  Plain text file Injector.php Class Main Class. PHP 7 compatible
  Plain text file Injector_5.php Class Main Class. PHP 7 Incompatible
  Accessible without login Plain text file readme.txt Doc. Read Me

 Version Control Unique User Downloads Download Rankings  
 100%
Total:370
This week:1
All time:6,807
This week:560Up