0

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!!

  • 1
    If you want to acces pimple methods from anywhere you are falling in service locator antipatern – jlvaquero Sep 30 '14 at 05:56
  • thanks for your reply. What you mean? I add new object to Pimple on one place, then I re-use the object on other place. Example I need to have access on Database Object from a Model, but also from a Library or from a plugin hook. So I need access to Pimple. Can you explain a bit more. Thanks!! – user2011250 Sep 30 '14 at 06:08
  • @jlvaquero can you tell me alternative? – user2011250 Sep 30 '14 at 06:20

1 Answers1

0

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];
    }
}
Dave Marshall
  • 7,367
  • 4
  • 22
  • 15
  • What I mean is that ABUSE of SL is antipattern. Using DI container everywhere is abuse. DI should build your dependency chain in the top layer. Then, use the layer and forguet about DI or SL. – jlvaquero Sep 30 '14 at 06:30
  • You said: "Then, use the layer and forguet about DI or SL." My reply: "What you mean with "layer" ? Can you provide an example how I can set database object and re-use him on hole application? thanks!!" – user2011250 Sep 30 '14 at 06:33