Permalink
Browse files

Added PHP tags for syntax highlighting.

  • Loading branch information...
1 parent c905cc2 commit bcf52b8d01731872aacc28753058e10e4f8af3c1 @inxilpro committed Jan 11, 2012
Showing with 20 additions and 0 deletions.
  1. +20 −0 Readme.markdown
View
20 Readme.markdown
@@ -8,8 +8,10 @@ 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:
```php
+<?php
require_once '/path/to/lib/Zit/Container.php';
$container = new \Zit\Container();
+?>
```
## Defining Objects
@@ -18,40 +20,50 @@ Like Pimple, objects are defined through anonymous functions that return an inst
of the object:
```php
+<?php
$container->set('auth', function() {
return new Auth();
});
+?>
```
All instantiation functions are passed the container as the first argument, making
dependency injection possible:
```php
+<?php
$container->set('auth', function($container) {
return new Auth($container->get('db'));
});
+?>
```
Zit also provides convenient magic methods for setting instantiation functions:
```php
+<?php
$container->setAuth(function() { // ... }); // Or:
$container->set_auth(function() { // ... });
+?>
```
## Getting Objects
Getting objects are as simple as:
```php
+<?php
$container->get('auth');
+?>
```
Or, if you prefer the shorthand:
```php
+<?php
$container->getAuth(); // Or:
$container->get_auth();
+?>
```
## Getting Fresh Objects
@@ -61,11 +73,13 @@ exact object is returned for each additional get(). If you need a fresh object,
do so with:
```php
+<?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
@@ -78,13 +92,15 @@ injecting dependencies. Zit automatically passes all parameters on to your inst
function:
```php
+<?php
$container->setUser(function($c, $id)) {
$user = new User($id);
$user->setDb($c->getDb());
return $user;
});
$user = $container->newUser(1);
+?>
```
## Storing Non-Objects
@@ -93,8 +109,10 @@ You can also use Zit to store non-objects (anything from a string to an anonymou
Just use the setParam() method:
```php
+<?php
$container->setParam('api_key', 'abcd1234567890');
$key = $container->get('api_key');
+?>
```
## Custom Container
@@ -103,6 +121,7 @@ Most projects will benefit from a custom container that sets up its own injectio
is as simple as extending Zit:
```php
+<?php
namespace MyApp\Di;
class Container extends \Zit\Container
@@ -113,6 +132,7 @@ class Container extends \Zit\Container
$this->setUser(function() { // ... });
}
}
+?>
```

0 comments on commit bcf52b8

Please sign in to comment.