Is this a good service locator, and is this service locator pattern(?) OK?

I’m working in Zend framework 1 and was using Zend_Registry to store objects so that I could replace them in my controllers while unit testing:

$auth = Zend_Registry::get('Auth'); // set in Bootstrap.php

I wanted to store closures that could be executed when they were required (rather than all during Booostrtap even if they weren’t used in that request), but Registry doesn’t seem to do this. I guess prior to 5.3 closures weren’t around?

So, I built my own service locator class. I thought to extend Zend_Registry but prefer to have something that can be used outside ZF1, plus it’s pretty basic anyway:

<?php

class App_Services
{
    /**
    * @var array $container will store our services
    */
    private $container = array();

    /**
    * Returns the *Singleton* instance of this class.
    *
    * @staticvar Singleton $instance The *Singleton* instances of this class.
    *
    * @return Singleton The *Singleton* instance.
    */
    public static function getInstance()
    {
        static $instance = null;
        if (null === $instance) {
            $instance = new App_Services();
        }

        return $instance;
    }

    /**
    * Get a service from the container
    * 
    * @param string $name Index of the service in container
    * @return various Service
    */
    public function get($id)
    {
        $instance = self::getInstance();

        // if not set, return null
        if (! isset($instance->container[$id]))
            return null;

        // if a closure, call it and return the return value
        if ($instance->container[$id] instanceof Closure) {
            $instance->container[$id] = $instance->container[$id]();
            return $instance->container[$id];
        }

        return $instance->container[$id];
    }

    /**
    * Set a service in the container
    * 
    * @param string $id Name (index) of the service in container
    * @param string $service Service to store
    */
    public function set($id, $service)
    {
        $instance = self::getInstance();

        $instance->container[$id] = $service;
    }
}

.. and on Bootstrap I can do:

$auth = App_Services::set('Auth', function() {
    return new Auth();
});

.. then within my controller actions I can do:

$auth = App_Service::get('Auth');

.. as before.

Anyway, is this a good implementation of a service locator? Also could either this or Zend_Registry used in this way, be regarded as a service locator? I’d like to understand the pattern better as I read service locator pattern is a good idea, then I read it’s an anti pattern, but I think some times it depends on how patterns are implemtemted whch makes then good patterns or anti patterns. I’d appreciate any advice on this, thanks.

I would change few things:

  1. remove singleton, make private static $container = [] anyway you’re just using static methods to retrieve it.
  2. consider throwing special exception when getting not set service.
  3. re-consider automatic caching for services (it is possible you may not want it for every service)

Considering pattern/anti pattern discussion I’ve found mostly cited is this article: http://blog.ploeh.dk/2010/02/03/ServiceLocatorisanAnti-Pattern/
which is summarized:

Let’s examine why this is so. In short, the problem with Service Locator is that it hides a class’ dependencies, causing run-time errors instead of compile-time errors, as well as making the code more difficult to maintain because it becomes unclear when you would be introducing a breaking change.

So basically it doesn’t concerns you – all your errors are run-time errors in PHP. (Or unitTests run time error if you are smart :D).

Anyway you can turn every pattern into it’s anti-version when overusing it. Think each time if some kind of dependency injection (DI) or other Inversion of Controll (IoC) fits you better.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật