Last active
August 29, 2015 13:56
-
-
Save shin1x1/9022968 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require __DIR__.'/vendor/autoload.php'; | |
class Foo | |
{ | |
/** | |
* @var string | |
*/ | |
protected $message; | |
/** | |
* @var string $message | |
*/ | |
public function __construct($message = '') | |
{ | |
$this->message = $message; | |
} | |
/** | |
* @return string | |
*/ | |
public function getMessage() | |
{ | |
return $this->message; | |
} | |
} | |
$app = new \Illuminate\Foundation\Application(); | |
// make | |
$foo = $app->make('Foo'); // message = '' | |
assert($foo->getMessage() === ''); | |
$foo = $app->make('Foo', ['Hello']); // message = 'Hello' | |
assert($foo->getMessage() === 'Hello'); | |
$foo = $app['Foo']; // message = '' | |
assert($foo->getMessage() === ''); | |
// bind | |
$app->bind('foo_bind', 'Foo'); | |
$foo = $app->make('foo_bind'); | |
assert($foo->getMessage() === ''); | |
$app->bind('foo_bind', function($app) { | |
return new Foo('foo_bind'); | |
}); | |
$foo = $app->make('foo_bind'); | |
assert($foo->getMessage() === 'foo_bind'); | |
$foo1 = $app->make('foo_bind'); | |
assert($foo !== $foo1); | |
// bindShared | |
$app->bindShared('foo_bind_shared', function($app) { | |
return new Foo('foo_bind_shared'); | |
}); | |
$foo1 = $app->make('foo_bind_shared'); | |
$foo2 = $app->make('foo_bind_shared'); | |
assert($foo1 === $foo2); | |
// bindIf | |
$app->bindIf('foo_bind', function($app) { | |
return new Foo('foo_bind_if'); | |
}); | |
$foo = $app->make('foo_bind'); // not changed | |
// instance | |
$app->instance('foo_instance', new Foo('instance')); | |
$foo = $app->make('foo_instance'); // message == 'instance' | |
assert($foo->getMessage() === 'instance'); | |
$foo1 = $app->make('foo_instance'); // message == 'instance' | |
assert($foo === $foo1); | |
// singleton | |
$app->singleton('foo_singleton', 'Foo'); | |
$foo1 = $app->make('foo_singleton'); // message == 'foo_singleton' | |
$foo2 = $app->make('foo_singleton'); // message == 'foo_singleton' | |
assert($foo1 === $foo2); | |
$app->singleton('foo_singleton_closure', function($app) { | |
return new Foo('foo_singleton_closure'); | |
}); | |
$foo1 = $app->make('foo_singleton_closure'); // message == 'foo_singleton_closure' | |
$foo2 = $app->make('foo_singleton_closure'); // message == 'foo_singleton_closure' | |
assert($foo1 === $foo2); | |
// alias | |
$app->alias('foo', 'bar'); | |
$bar = $app->make('bar'); // message == 'foo' | |
assert(get_class($bar) === 'Foo'); | |
// array | |
$app['foo_set'] = function() { | |
return new Foo('foo_set'); | |
}; | |
$foo = $app->make('foo_set'); | |
assert($foo->getMessage() === 'foo_set'); | |
// extend | |
$app->extend('foo_bind', function($foo, $app) { | |
$foo->extend = 'ex'; | |
return $foo; | |
}); | |
$foo = $app->make('foo_bind'); // message == 'foo_bind', extend = 'ex' | |
assert($foo->getMessage() === 'foo_bind'); | |
assert($foo->extend === 'ex'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment