5.2. Using S2Container in component

When the component is dependent on S2Container into which it is registered, you can get S2Container by Property Injection or Setter Injection. The component name of S2Container is "container".
Let's create following service classes as an example. S2Container instance is going to be injected by automatic Property Injection.

class Service {
    public $container = null;
    public function add($a, $b) {
        return $a + $b;
    }
}

An execution script is created as follows.

<?php
require_once('S2Container/S2Container.php');

use seasar\container\S2ApplicationContext as s2app;
s2app::import(dirname(__FILE__) . '/classes');
$service = s2app::get('Service');

print get_class($service->container) . PHP_EOL;

[Note]NOTE

This Example is located in examples/misc/s2container.



© Copyright The Seasar Foundation and the others 2005-2010, all rights reserved.