Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Passing multiple dependencies
- */
- //Create the dependencies or retrieve them from the registry
- $connection = new Connection();
- $session = new Session();
- $fileSystem = new FileSystem();
- $filter = new Filter();
- $selector = new Selector();
- //Pass them as constructor parameters
- $some = new SomeComponent($connection, $session, $fileSystem, $filter, $selector);
- // ... or using setters
- $some->setConnection($connection);
- $some->setSession($session);
- $some->setFileSystem($fileSystem);
- $some->setFilter($filter);
- $some->setSelector($selector);
- /**
- * Using a DIC to set the dependencies and pass it as a single argument
- */
- $di = new DIC();
- //Register a "db" service in the container
- $di->set('db', function() {
- return new Connection(array(
- "host" => "localhost",
- "username" => "root",
- "password" => "secret",
- "dbname" => "invo"
- ));
- });
- //Register a "filter" service in the container
- $di->set('filter', function() {
- return new Filter();
- });
- //Register a "session" service in the container
- $di->set('session', function() {
- return new Session();
- });
- //Pass the service container as unique parameter
- $some = new SomeComponent($di);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement