class FactoryFoo implements FactoryInterface
{
    public function createService(ServiceLocatorInterface $locator, $cName, $rName)
    {
        return new Service();
    }
}
class Service
{
}
function FactoryFunction(ServiceLocatorInterface $locator, $cName, $rName)
{
    return new Service();
}
// setup ServiceLocator
$serviceLocator = new ServiceLocator();
// factory as closure
$serviceLocator->setFactory('service1', function (ServiceLocatorInterface $locator) {
    return Service();
});
// factory as closure
$serviceLocator->setFactory('service2', 'FactoryFunction');
// factory as class
$serviceLocator->setFactory('service3', '\\Example\\FactoryFoo');
// factory as instance
$serviceLocator->setFactory('service4', new FactoryFoo());
// get a service
$service1 = $serviceLocator->get('service1');
$service2 = $serviceLocator->get('service2');
$service3 = $serviceLocator->get('service3');
$service4 = $serviceLocator->get('service4');
class ServiceCanHoldServiceLocator3
{
    protected $serviceLocator;
    public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
    {
        $this->serviceLocator = $serviceLocator;
    }
    public function getServiceLocator()
    {
        return $this->serviceLocator;
    }
}
// setup ServiceLocator
$serviceLocator = new ServiceLocator();
// Hold with trait
$serviceLocator->setFactory('service1', function (ServiceLocatorInterface $locator) {
    return new ServiceCanHoldServiceLocator1();
});
// Hold with interface
$serviceLocator->setFactory('service2', function (ServiceLocatorInterface $locator) {
    return new ServiceCanHoldServiceLocator1();
});
// Hold with custom
$serviceLocator->setFactory('service3', function (ServiceLocatorInterface $locator) {
    $service = new ServiceCanHoldServiceLocator3();
    $service->setServiceLocator($locator);
    return $service;
});
$serviceLocator->get('service1');
$serviceLocator->get('service2');
$serviceLocator->get('service3');