set() публичный Метод

Set a service object. It will overwrite any previous service with the same name.
public set ( string $name, mixed $call, mixed $params = null ) : Locator
$name string
$call mixed
$params mixed
Результат Locator
Пример #1
0
 /**
  * Set a service
  *
  * @param  string $name
  * @param  mixed  $call
  * @param  mixed  $params
  * @return \Pop\Project\Project
  */
 public function setService($name, $call, $params = null)
 {
     $this->services->set($name, $call, $params);
     return $this;
 }
Пример #2
0
<?php

require_once '../../bootstrap.php';
use Pop\Service\Locator;
class Foo
{
    public function bar($val)
    {
        return new \Pop\Config(array('test' => $val));
    }
    public static function baz($val)
    {
        return new \Pop\Config(array('test' => $val));
    }
}
try {
    $locator = new Locator();
    // Load the services manually via the setter
    $locator->set('config1', 'Foo->bar', array(123))->set('config2', 'Foo::baz', array(456));
    // Get a service
    print_r($locator->get('config1'));
    print_r($locator->get('config2'));
    print_r($locator);
} catch (\Exception $e) {
    echo $e->getMessage() . PHP_EOL . PHP_EOL;
}
Пример #3
0
 public function testClassWithParams()
 {
     $l = new Locator();
     $l->set('config1', 'PopTest\\Service\\Foo', array(123))->set('config2', 'PopTest\\Service\\Foo->bar', array(456))->set('config3', 'PopTest\\Service\\Foo::baz', array(789));
     $c1 = $l->get('config1');
     $c2 = $l->get('config2');
     $c3 = $l->get('config3');
     $this->assertInstanceOf('PopTest\\Service\\Foo', $c1);
     $this->assertInstanceOf('Pop\\Config', $c2);
     $this->assertInstanceOf('Pop\\Config', $c3);
 }
Пример #4
0
<?php

require_once '../../bootstrap.php';
use Pop\Service\Locator;
try {
    $locator = new Locator();
    // Load the services manually via the setter
    $locator->set('config', 'Pop\\Config', array(array('test' => 123)))->set('color', 'Pop\\Color\\Color', array(new \Pop\Color\Rgb(255, 0, 0)));
    // Get a service
    print_r($locator->get('config'));
    // Remove a service
    $locator->remove('color');
    print_r($locator);
} catch (\Exception $e) {
    echo $e->getMessage() . PHP_EOL . PHP_EOL;
}