2

Basic requirement

I'm making a framework for learning purposes (and likely usage on personal sites).

I'm using dependency injection in classes and I'm trying to design where would be a good place to instantiate dependency objects.
Note: This is in the framework, not front end (so not templates etc).

So when a class has a dependency from another class or a configuration file (config files are classes), I need to instantiate the dependency object somewhere to be able to pass it into the new class (DI).

I've managed to create logical places for all other things within the framework, but cannot seem to work out and design a logical and purposeful place to instantiate dependency objects.

Framework structure

The basic structure of my framework is:

  1. htaccess sends all requests to index.php
  2. index.php includes() bootstrap.php
  3. bootstrap.php loads all framework components

"Components" are things like SPL Autoload Register, setting up error display with ini_set(), other things.

All these components are in separate files and are called by bootstrap.php.

There are a number of system components, but bootstrap.php loads much more, a lot of which will require a dependency injecting in. So this is not a pointless exercise for little return.

Currently doing this in bootstrap.php

I'm currently instantiating the dependency objects in bootstrap.php prior to instantiating the new system class. However I feel this is making bootstrap.php a bit messy, cluttered, and such things should perhaps live else where (e.g. in a dependency layer, and dependency namespace, not bootstrap namespace).

Example

Example of a system class with dependencies in bootstrap.php, it's a class which manages if errors are displayed or not:

(Class autoloading is on.)

bootstrap.php:

// Get the dependency from config file
$errorConfig = new errorConfig();
$dependency = $errorConfig->getErrorConfig();

// Instantiate system file, injecting config dependency from above
$displayErrors = new displayErrors();
$run = $displayErrors->runDisplayErrors($dependency);

displayErrors.php:

class displayErrors {

  public function runDisplayErrors($dependency) {
    ini_set('display_errors', $dependency);
  }

}

So $dependency in this case will be a value from the config file of either "on" or "off".

config.php:

class errorConfig {

  public function getErrorConfig() {
    return "on";
  }

}

So in bootstrap.php I am instantiating the class which holds the dependency (such as a config class), calling the dependency class method to get the dependency, and passing the object into the class which requires the dependency.

NOTE: The way I am actually retrieving the config data is being worked on, that is irrelevant here as it will be an object, and I'm asking about where to instantiate the config class to get the object, not how.

Doing it this way means bootstrap.php is full of dependencies, and this is not what bootstrap.php should be for (based on my opinion and design ideas, I could be wrong).
bootstrap.php is a loader file which is just for loading the systems (ie setting up error display), and not for instantiating and managing dependencies.

Dependency injection container

I could use a dependency injection container (DIC) (and I am reading about the advantages etc) but as far as my current knowledge of DIC shows, this does not resolve where to instantiate the dependency objects.
A DIC would just move the issue around, and I'd just be instantiating the DIC within bootstrap.php instead of the dependency class directly.

My questions

In terms of application architecture and design, where would it be good to instantiate dependency objects, or even a DIC, to keep things clean, organised, and tidy?

Do I need to add some layer which bootstrap.php calls once and then dependency objects are instantiated within that layer?

If you feel instantiating the dependency objects within bootstrap.php is ok, please let me know. I'd be interested in another view on this. Personally, I'm against it as it makes bootstrap.php smell, and bends its purpose.

1 Answer 1

2

Most dependency injection containers can be configured to create the dependencies as well as inject them. No need to have a separate process to create dependencies.

Here is a simple example using the PHP Pimple container(http://pimple.sensiolabs.org/)

// This is the importer service definition
$dic['arbiter_schedule_importer_games_with_slots_xml'] = function() use ($dic) {
  return new ImporterGamesWithSlotsXml(
    $dic['arbiter_db_conn']
  );
};
// This is the import command definition
$dic['arbiter_schedule_import_command'] = function() use ($dic) {
  return new ImportCommand(
    $dic['arbiter_schedule_importer_games_with_slots_xml']
  );

// This creates the command and creates/injects the importer service dependency.
$importCommand = $dic->get('arbiter_schedule_import_command');

And yes, you do have to create and configure the container somewhere but that should be it as far as bootstrapping goes.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.