Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Basic di-container for php.

branch: master

Merge pull request #8 from Ikke/add_factory_checks

Added checks to the create method to check if the factories actually return something
latest commit 2f47b3df1c
Troels Knak-Nielsen authored
Octocat-spinner-32 lib Added checks to the create method to check if the factories actually … May 23, 2011
Octocat-spinner-32 LICENSE Specified BSD license for Bucket June 26, 2013
Octocat-spinner-32 README.markdown
Octocat-spinner-32 bucket.test.php Fixes issue #4 February 22, 2010
README.markdown

Bucket - Basic di-container for php.

Bucket is a very minimal, yet useful di-container for PHP. It's easy to get started with and offers an open ended migration-path to a more full-featured framework, should you get the need later.

Unlike many other containers, Bucket doesn't have a very intelligent factory. This means no configuration, and a very short learning-path. It also means that you can use Bucket as a front-end for a more sophisticated di-container.

Bucket is a simple container that delegates creation to an external factory. It has default creational-logic, that relies on typehints+reflection, which is usually adequate for simpler applications. The container completely hides the factory, so as the application complexity grows, you can attach a more sophisticated factory to the container, without having to change your application code.

Basic usage of Bucket

Test classes:

interface Zap {}
class Foo implements Zap {}
class Bar {
  function __construct(Foo $foo) {}
}
class Cuux {
  function __construct(Zap $zap) {}
}

To instantiate a class:

$bucket = new bucket_Container();
$foo = $bucket->create('Foo');

To get a shared instance of a class:

$bucket = new bucket_Container();
$foo = $bucket->get('Foo');

Dependencies

Bucket can resolve simple dependencies (concrete-class typehints):

$bucket = new bucket_Container();
$bar = $bucket->get('Bar');

Bucket can also resolve interface type-hints, if you specify the implementation to use:

$bucket = new bucket_Container();
$bucket->registerImplementation('Zap', 'Foo');
$bar = $bucket->get('Cuux');

Factories

If you need more complicated creational logic, you can attatch a factory to the container:

class MyFactory {
  function new_PDO($container) {
    return new PDO("mysql:host=localhost;dbname=addressbook", "root", "secret");
  }
}

$bucket = new bucket_Container(new MyFactory());
$db = $bucket->get('pdo');

The container is passed to factories, so it can resolve further dependencies.

Bucket also supports callbacks for factories. This isn't very useful at the moment, but once PHP 5.3 is released, you can use anonymous functions for registering factories:

// Requires php 5.3+
$factory = new StdClass();
$factory->new_pdo = function($container) {
  return new PDO("mysql:host=localhost;dbname=addressbook", "root", "secret");
}
$bucket = new bucket_Container($factory);

or:

// Requires php 5.3+
$bucket = new bucket_Container(
  array(
    'pdo' => function($container) {
      return new PDO("mysql:host=localhost;dbname=addressbook", "root", "secret");
    }
  )
);

Scopes

Bucket supports nested scopes for fine-tuned management of lifecycles.

$top = new bucket_Container();
$scope = $top->makeChildContainer();
$bar = $scope->get('Cuux');

In the above example, state is maintained on the scoped container $scope - not the on $top.

Alternatives

If Bucket didn't suit your needs, you might prefer one of the following:

Something went wrong with that request. Please try again.