简体   繁体   中英

Pimple dependency injection static or object

Pimple help re-use the same object across application, and manage dependecy.

but how to manage Pimple itself?

Should I create a global object? Or make it static class? Or use a function?

I would like to access on Pimple methods from anywhere, controllers, models, plugins, etc...

Thanks!!

A lot of folk consider ServiceLocator to be an anti-pattern, but if you use it sparingly, there's little harm done.

<?php

namespace Acme;

class ServiceLocator
{
    static protected $container;

    public static function setContainer(\Pimple $container)
    {
        static::$container = $container;
    }

    public static function get($id)
    {
        return static::$container[$id];
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM