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

Get a service object.
public get ( string $name ) : mixed
$name string
Результат mixed
Пример #1
0
<?php

require_once '../../bootstrap.php';
use Pop\Service\Locator;
try {
    // Load the services config via the constructor
    $locator = new Locator(array('config' => array('call' => 'Pop\\Config', 'params' => array(array('test' => 123), true)), 'rgb' => array('call' => 'Pop\\Color\\Rgb', 'params' => function () {
        return array(255, 0, 0);
    }), 'color' => function ($locator) {
        return new \Pop\Color\Color($locator->get('rgb'));
    }));
    // Services have not been loaded/instantiated yet
    print_r($locator);
    // Get the services as you need them
    print_r($locator->get('config'));
    print_r($locator->get('rgb'));
    print_r($locator->get('color'));
    // Now the services are loaded/instantiated within the locator object
    print_r($locator);
} catch (\Exception $e) {
    echo $e->getMessage() . PHP_EOL . PHP_EOL;
}
Пример #2
0
 /**
  * Get a service
  *
  * @param  string $name
  * @return mixed
  */
 public function getService($name)
 {
     return $this->services->get($name);
 }
Пример #3
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;
}
Пример #4
0
 public function testRecursiveException()
 {
     $this->setExpectedException('Pop\\Service\\Exception');
     $l = new Locator(array('service1' => function ($locator) {
         return $locator->get('service2');
     }, 'service2' => function ($locator) {
         return $locator->get('service1');
     }));
     $s = $l->get('service1');
 }
Пример #5
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;
}