Twittee is the smallest, and still useful, Dependency Injection Container in PHP

Overview

What is Twittee?

Twittee is the smallest, and still useful, Dependency Injection Container in PHP; it is also probably one of the first public software to use the newest anonymous functions support of PHP 5.3.

Packed in less than 140 characters, it fits in a tweet.

Despite its size, Twittee is a full-featured Dependency Injection Container with support for object definitions, object injection and parameters.

Published in 2009 by Fabien Potencier, Twittee is in the Public Domain. Tweet me if you find a bug!

Usage

Finding a simple example to demonstrate a Dependency Injection Container is not an easy task. Instead of showing a classic "Hello World!" example, which would have been too simple to demonstrate the power of Twittee, I have converted the example I used to introduce the Symfony 2 Dependency Injection Container on my blog.

The following example shows how to create a Zend_Mail object that sends its emails using a Gmail account:

$c = new Container();

// parameters
$c->mailer_class = function () { return 'Zend_Mail'; };
$c->mailer_username = function () { return 'fabien'; };
$c->mailer_password = function () { return 'myPass'; };

// objects / services
$c->mailer_transport = function ($c) {
  return new Zend_Mail_Transport_Smtp(
    'smtp.gmail.com',
    array(
      'auth'     => 'login',
      'username' => $c->mailer_username,
      'password' => $c->mailer_password,
      'ssl'      => 'ssl',
      'port'     => 465,
    )
  );
};
$c->mailer = function ($c) {
  $obj = new $c->mailer_class();
  $obj->setDefaultTransport($c->mailer_transport);
  return $obj;
};

// get the mailer
$mailer = $c->mailer;

Some explanations about the code are in order:

  • Parameters are defined by anonymous functions that return strings;

  • Objects/services are defined by anonymous functions that return object instances;

  • Links between objects and parameters access are done by accessing the container, which is passed to the anonymous function as an argument.

Looking for a "real" Dependency Injection Container for PHP?

Try the Symfony Service Container.

Do you like Twittee?

If you like Twittee, you will also probably like Twitto, the Web Framework that fits in a tweet!

You might also like...
DI Container (PSR-11)

My DI Container It's my own implementation PSR-11 Container Interface. Installation composer require scruwi/container Init $container = new Container(

This repository holds all interfaces related to PSR-11 (Container Interface).

Container interface This repository holds all interfaces related to PSR-11 (Container Interface). Note that this is not a Container implementation of

Extensible DI container for Symfony2
Extensible DI container for Symfony2

This package contains an implementation of the Symfony 2 DI container that can be extended using other DI containers (from other frameworks).

The DependencyInjection component allows you to standardize and centralize the way objects are constructed in your application.

DependencyInjection Component The DependencyInjection component allows you to standardize and centralize the way objects are constructed in your appli

Adapters for PHP framework containers to an interoperable interface

Acclimate - Container Adapters Get Acclimated! Use any third-party dependency injection containers and service locators in your code by adapting them

πŸ’Ž Flexible, compiled and full-featured Dependency Injection Container with perfectly usable autowiring and support for all new PHP 7 features.

Nette Dependency Injection (DI) Introduction Purpose of the Dependecy Injection (DI) is to free classes from the responsibility for obtaining objects

πŸš€ PHP Service Container with fast and cachable dependency injection.
πŸš€ PHP Service Container with fast and cachable dependency injection.

ClanCats Container A PHP Service Container featuring a simple meta-language with fast and compilable dependency injection. Requires PHP = 7.0 Pros: M

A small PHP dependency injection container

Pimple Caution! Pimple is now closed for changes. No new features will be added and no cosmetic changes will be accepted either. The only accepted cha

PSR-11 compatible Dependency Injection Container for PHP.

bitexpert/disco This package provides a PSR-11 compatible, annotation-based dependency injection container. Have a look at the disco-demos project to

The dependency injection container for humans
The dependency injection container for humans

layout home PHP-DI is a dependency injection container meant to be practical, powerful, and framework-agnostic. Read more on the website: php-di.org G

Small but powerful dependency injection container

Container (Dependency Injection) This package is compliant with PSR-1, PSR-2, PSR-4 and PSR-11. If you notice compliance oversights, please send a pat

YCOM Impersonate. Login as selected YCOM user πŸ§™β€β™‚οΈin frontend.

YCOM Impersonate Login as selected YCOM user in frontend. Features: Backend users with admin rights or YCOM[] rights, can be automatically logged in v

Phalcon PHP REST API Package, still in beta, please submit issues or pull requests

PhREST API A Phalcon REST API package, based on Apigees guidelines as found in http://apigee.com/about/content/web-api-design Please see the skeleton

Dependency Injection System

Aura.Di A serializable dependency injection container with constructor and setter injection, interface and trait awareness, configuration inheritance,

Dependency Injection System

Aura.Di A serializable dependency injection container with constructor and setter injection, interface and trait awareness, configuration inheritance,

Provide a module to industrialize API REST call with dependency injection using Guzzle library
Provide a module to industrialize API REST call with dependency injection using Guzzle library

Zepgram Rest Technical module to industrialize API REST call with dependency injection using Guzzle library. Provides multiple features to make your l

Yii Dependency Injection PSR-11 compatible

Yii Dependency Injection PSR-11 compatible dependency injection container that is able to instantiate and configure classes resolving dependencies. Fe

Value Object that represents a monetary value (using a currency's smallest unit).

This project has been abandoned. It was only ever intended to be used as an example for PHPUnit features etc. and not for usage in production. I am so

Quite possibly the smallest MVC framework you'll ever use.

Swiftlet Swiftlet is quite possibly the smallest MVC framework you'll ever use. And it's swift. Licensed under the MIT license. Buzzword compliance βœ”

πŸš€ PHP Service Container with fast and cachable dependency injection.

ClanCats Container A PHP Service Container featuring a simple meta-language with fast and compilable dependency injection. Requires PHP >= 7.0 Pros: M

ClanCats 28 Apr 13, 2022
A small PHP dependency injection container

Pimple Caution! Pimple is now closed for changes. No new features will be added and no cosmetic changes will be accepted either. The only accepted cha

Silex 2.6k Dec 29, 2022
PSR-11 compatible Dependency Injection Container for PHP.

bitexpert/disco This package provides a PSR-11 compatible, annotation-based dependency injection container. Have a look at the disco-demos project to

bitExpert AG 137 Sep 29, 2022
The dependency injection container for humans

layout home PHP-DI is a dependency injection container meant to be practical, powerful, and framework-agnostic. Read more on the website: php-di.org G

null 2.4k Jan 4, 2023
Small but powerful dependency injection container

Container (Dependency Injection) This package is compliant with PSR-1, PSR-2, PSR-4 and PSR-11. If you notice compliance oversights, please send a pat

The League of Extraordinary Packages 779 Dec 30, 2022
Dependency Injection System

Aura.Di A serializable dependency injection container with constructor and setter injection, interface and trait awareness, configuration inheritance,

Aura for PHP 342 Dec 1, 2022
Yii Dependency Injection PSR-11 compatible

Yii Dependency Injection PSR-11 compatible dependency injection container that is able to instantiate and configure classes resolving dependencies. Fe

Yii Software 161 Nov 10, 2022
Dependency Manager for PHP

Composer - Dependency Management for PHP Composer helps you declare, manage, and install dependencies of PHP projects. See https://getcomposer.org/ fo

Composer 27.2k Dec 31, 2022
IoC Dependency Injector

auryn auryn is a recursive dependency injector. Use auryn to bootstrap and wire together S.O.L.I.D., object-oriented PHP applications. How It Works Am

Daniel Lowrey 725 Nov 23, 2022
IoC Dependency Injector

auryn auryn is a recursive dependency injector. Use auryn to bootstrap and wire together S.O.L.I.D., object-oriented PHP applications. How It Works Am

Daniel Lowrey 710 Apr 15, 2021