packfire/fuelblade

This package is abandoned and no longer maintained. No replacement package was suggested.

Packfire FuelBlade - Dependency Injection / IoC library for PHP

1.2.3 2014-03-13 11:22 UTC

This package is not auto-updated.

Last update: 2021-05-14 20:45:57 UTC


README

#Packfire FuelBlade

##What is FuelBlade?

Packfire FuelBlade is a library that helps you the power of dependency injection into your PHP application. Through the Inversion of Control (IoC) technique, you can decouple class dependencies and build better test-friendly code.

##What is IoC?

Traditionally, a class is easily coupled like this:

    class ConsoleOutput
    {
        public function write($message)
        {
            echo $message;
        }        
    }

    class TaskManager
    {
        protected $output;

        public function __construct()
        {
            $this->output = new ConsoleOutput();
        }
    }

    class Application
    {
        public function run()
        {
            $manager = new TaskManager();
            $manager->run();                  
        }
    }

However, TaskManager is now coupled to ConsoleOutput. Any output made by TaskManager can only go to console output and there is no flexibility in choosing which output to use. Hence, we can perform some abstraction magic and write some code like this:

    interface OutputInterface
    {
        public function write($message);
    }

    class ConsoleOutput implements OutputInterface
    {
        public function write($message)
        {
            echo $message;
        }        
    }

    class FileOutput implements OutputInterface
    {
        public function write($message)
        {
            // write to file
        }        
    }

    class TaskManager
    {
        protected $output;

        public function __construct(OutputInterface $output)
        {
            $this->output = $output;
        }

    }

    class Application
    {
        public function run()
        {
            $ioc = new \Packfire\FuelBlade\Container();
            $ioc['manager'] = $this->share(
                function ($ioc) {
                    return new TaskManager($ioc['output']);
                }
            );
            $ioc['output'] = $this->share(new FileOutput());
            $ioc['manager']->run();                    
        }
    }

Nettuts+ has a great article on explaining the various methods of dependency injections, and ultimately explaining IoC as the ultimate solution.

##Sounds Great! How do I install FuelBlade?

You can install FuelBlade from Packagist via Composer.

{
    "require": {
        "packfire/fuelblade": "1.1.*"
    }
}

All of the releases of Packfire FuelBlade are described on the package's Packagist page. Through Composer's autoloader (or your own), you will be able to use FuelBlade directly into your application.