Permalink
Browse files

Added GitHub syntax highlighting.

  • Loading branch information...
inxilpro committed Jan 10, 2012
1 parent 5ec8740 commit c905cc2c889b813ee893262afea09e5f4d7906e2
Showing with 55 additions and 38 deletions.
  1. +55 −38 Readme.markdown
View
@@ -7,54 +7,66 @@ same simplicity as Pimple while offering a slightly more robust object interface
Zit is simple to use. Just include it and create a new container:
- require_once '/path/to/lib/Zit/Container.php';
- $container = new \Zit\Container();
+```php
+require_once '/path/to/lib/Zit/Container.php';
+$container = new \Zit\Container();
+```
## Defining Objects
Like Pimple, objects are defined through anonymous functions that return an instance
of the object:
- $container->set('auth', function() {
- return new Auth();
- });
+```php
+$container->set('auth', function() {
+ return new Auth();
+});
+```
All instantiation functions are passed the container as the first argument, making
dependency injection possible:
- $container->set('auth', function($container) {
- return new Auth($container->get('db'));
- });
+```php
+$container->set('auth', function($container) {
+ return new Auth($container->get('db'));
+});
+```
Zit also provides convenient magic methods for setting instantiation functions:
- $container->setAuth(function() { // ... });
- // Or, if you prefer underscores:
- $container->set_auth(function() { // ... });
+```php
+$container->setAuth(function() { // ... }); // Or:
+$container->set_auth(function() { // ... });
+```
## Getting Objects
Getting objects are as simple as:
- $container->get('auth');
+```php
+$container->get('auth');
+```
Or, if you prefer the shorthand:
- $container->getAuth();
- // Or:
- $container->get_auth();
+```php
+$container->getAuth(); // Or:
+$container->get_auth();
+```
## Getting Fresh Objects
By default, all objects are shared in Zit. That is, once an object is created, that same
exact object is returned for each additional get(). If you need a fresh object, you can
do so with:
- $container->fresh('auth'); // Or:
- $container->freshAuth(); // Or:
- $container->fresh_auth(); // Or:
- $container->newAuth(); // Or:
- $container->new_auth();
+```php
+$container->fresh('auth'); // Or:
+$container->freshAuth(); // Or:
+$container->fresh_auth(); // Or:
+$container->newAuth(); // Or:
+$container->new_auth();
+```
Note the because the 'new' keyword is reserved, you can only use it if you're using
the magic methods.
@@ -65,37 +77,42 @@ Sometimes you need to pass parameters to the constructor of an object, while sti
injecting dependencies. Zit automatically passes all parameters on to your instantiation
function:
- $container->setUser(function($c, $id)) {
- $user = new User($id);
- $user->setDb($c->getDb());
- return $user;
- });
-
- $user = $container->newUser(1);
+```php
+$container->setUser(function($c, $id)) {
+ $user = new User($id);
+ $user->setDb($c->getDb());
+ return $user;
+});
+
+$user = $container->newUser(1);
+```
## Storing Non-Objects
You can also use Zit to store non-objects (anything from a string to an anonymous function).
Just use the setParam() method:
- $container->setParam('api_key', 'abcd1234567890');
- $key = $container->get('api_key');
+```php
+$container->setParam('api_key', 'abcd1234567890');
+$key = $container->get('api_key');
+```
## Custom Container
Most projects will benefit from a custom container that sets up its own injection rules. This
is as simple as extending Zit:
- namespace MyApp\Di;
-
- class Container extends \Zit\Container
+```php
+namespace MyApp\Di;
+
+class Container extends \Zit\Container
+{
+ public function __construct()
{
- public function __construct()
- {
- $this->setAuth(function() { // ... });
- $this->setUser(function() { // ... });
- }
+ $this->setAuth(function() { // ... });
+ $this->setUser(function() { // ... });
}
-
+}
+```

0 comments on commit c905cc2

Please sign in to comment.