Support the ongoing development of Laravel.io →
posted 10 years ago
IOC
Last updated 2 years ago.
0

Welcome!

It's ugly...and feels like a hack...but possible I think..

You can pass a parameter when calling App::make. So basically the closure works as a factory.

interface Test
{

}

class Test1 implements Test
{

}

class Test2 implements Test
{

}

App::bind('Test', function($app, $param = null) {

    $classes = ['Test1', 'Test2'];

    if ($param && in_array($param,$classes)) {
        return App::make($param);
    }

    return [
        'Test1' => new Test1,
        'Test2' => new Test2
    ];

});

Route::get('/', function(){
    
    // to get all Test1 and Test2
    $test = App::make('Test');

   // to get only Test1
   $test = App::make('Test', 'Test1')
    dd($test);
    exit;

});

It doesn't work magically like c#, but I think you get the idea.

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Daniel15 daniel15 Joined 19 May 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.