Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Fuel DiC in 1.2
  • I need a DiC to handle dependencies in my FuelPHP 1.2 application and have been considering using the new FuelPHP DiC to make migration to 2.0 easier. I have also been looking at Laravel's IoC class and fabpot/Pimple. It was very easy to get FuelPHP DiC working in 1.2 but it seems to be a bit different and slightly more complex than the other containers I've been looking e.g. In Pimple to register a singleton I can do this:
    $container['session'] = $container->share(function ($c) {
        return new Session($c['session_storage']);
    });
    

    Then access it using array access In FuelPHP's DiC, would I use setClass and getObject to do the same thing, or am I using it incorrectly?
    $dic->setClass('session', function() use ($dic) {
        return new Session($dic->getObject('session_storage'));
    });
    
    $obj = $dic->getObject('session');
    

    Thanks
  • Hadn't considered implementing arrayaccess for the DiC yet, it's a good idea. If you make a feature request on Github I'll implement it over the weekend.
  • Jelmer Schreuder wrote on Thursday 2nd of August 2012:
    Hadn't considered implementing arrayaccess for the DiC yet, it's a good idea. If you make a feature request on Github I'll implement it over the weekend.

    Done I'm still not sure whether my example above is using the DiC correctly though. Am I using setClass and getObject correctly? Should I be passing $dic to the closure? Thanks
  • Looks fine to me, you can take a look in the fuelphp/kernel and fuelphp/core repos to see how I've implemented it in the 2.0 repos (the loader.php files). Which is pretty much te same other than the use of uppercase characters (which doesn't matter as the DiC is case insensitive).
  • Thanks for adding this feature so quickly. I had a look at the kernel and core and found some good examples in loader.php. Thanks very much for the help
  • Turned out to be a pretty easy addition, wrote it on my tablet on the busride home from work. :D
  • I'm still finding the DiC challenging to implement. I'm trying to register scalars to use as parameters. For example:
    $dic->setClass('Foo', function() use ($dic) {
        return new Foo("bar");
    });
    

    How would I register the string "bar" in the container? setClass appears to work but I think I'm using this incorrectly, because it's not actually a class:
    $dic->setClass("bar", "bar");
    $dic->setClass('Foo', function() use ($dic) {
        return new Foo($dic->getClass("bar"));
    });
    

    Thanks for the help
  • I have no clue what you are trying to accomplish? You are registering the string "bar" as a class and you're surprised that doesn't work? This looks more like you'd want to register an object:
    $dic->setClass('Foo', function($param) use ($dic) {
        return new Foo($param);
    });
    $dic->setObject('Foo', 'bar', $dic->forge('Foo', 'bar'));
    

    Did you read the README? The code you attempted makes no sense to me, so I'm kinda hoping it's not something that looks likely to anyone after reading that. [edit:] But just in case and to clarify: the DiC is no replacement for configuration. It's meant to be able to generate objects and keep a registry of those. Keeping a collection of values is not a functionality that belongs in the DiC, it's something you'd use configuration for.
  • Jelmer Schreuder wrote on Tuesday 7th of August 2012:
    [edit:] But just in case and to clarify: the DiC is no replacement for configuration. It's meant to be able to generate objects and keep a registry of those. Keeping a collection of values is not a functionality that belongs in the DiC, it's something you'd use configuration for.

    Sorry for the confusion, it's just a bit different to the other containers I have looked at which also store parameters. I'm not trying to replace configuration, just register some parameters. This is how I would do it in Zend, and Symfony is similar as well:
    $di = new Zend\Di\DependencyInjector;
    $di->getInstanceManager()->setParameters('Foo', array(
        'bar' => 'bar'
    );
    $foo = $di->get('Foo');
    

    So whenever I instantiate Foo, or another class that depends on Foo, the "bar" parameter will be injected automatically With Fuel's DiC, if I understand your reply correctly, I have to set the parameters when forging an object
    $dic->forge(array('Foo', 'foo'), 'bar');
    

    But if I have class "Baz" that depends on "Foo", I don't want to have to inject Foo every time I forge it. So I could inject a Foo object by passing $dic->getObject('Foo', 'foo') as the parameter:
    $dic->setClass('Baz', function() use ($dic) {
      return new Baz($dic->getObject('Foo', 'foo'));
    });
    

    I have read the README carefully, but couldn't see any example like this. Have I used the DiC correctly in this example? Thanks for the help
  • I'd do that like this:
    $dic->setClasses(array(
        'Foo' => function($param = 'bar') {
            return new Foo($param);
        },
        'Baz' => function() use ($dic) {
             return new Baz($dic->forge('Foo'));
        },
    ));
    

    Though as Foo can be called without params $dic->getObject('Foo') will work as well (auto instantiates without params). I don't really see the point of adding default-params support as closures solve that more elegantly and offer more flexebility.
  • Makes sense. Thanks for your help again

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion