Giter Club home page Giter Club logo

twittee's Introduction

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!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.