10

I am trying to follow the repository pattern outlined in this article http://code.tutsplus.com/tutorials/the-repository-design-pattern--net-35804#highlighter_174798 And I am trying to instantiate a class in Laravel using App::make() (Which I am guessing is Laravel's factory pattern?) and I am trying to parse arguments to my class but I can't work out how to do it.

Code:

namespace My;

class NewClass {
    function __construct($id, $title) 
    {
        $this->id = $id;
        $this->title = $title;
    }
}

$classArgs = [
    'id'    => 1,
    'title' => 'test',
]

$newClass = App::make('My\NewClass', $classArgs);

Can anyone point to an example of how to use App::make() or have I gone in the completely wrong direction and shouldn't be using App::make()?

Laurence
  • 58,936
  • 21
  • 171
  • 212
Theo Kouzelis
  • 3,195
  • 5
  • 37
  • 66

2 Answers2

14

App is actually a facade for Laravel IoC container usually used for automatic resolution. Understanding of IoC concept is vital for complex application development but small projects will benefit from well architecture for sure. I would recommend to dive into Laravel documentation first and try some examples on Service Providers, Bindings and Automatic Resolution.

Speaking about your example:

namespace My;

class NewClass {

    function __construct($id, $title) 
    {
        $this->id    = $id;
        $this->title = $title;
    }
}


$newClass = App::make('My\NewClass', [1, 'test']);
  • Thanks for your reply. So you wouldn't use App::make() to pass an array of arguments, because that seems to be the functionality of View::make('SomeView', $args); – Theo Kouzelis Feb 10 '14 at 17:56
  • Sure. I would say its more for structuring application and adding some level of abstraction. For example I use it to resolve interfaces (bound in service provider). `App::make('My\ClassInterface');` –  Feb 10 '14 at 18:02
  • 1
    Whats the benefit of it? whats the difference if i use `new NewClass(1, 'test')` – Brand Guy Apr 16 '19 at 00:14
  • @BrandGuy it's fine to use `new Class` but that will return a new instance everytime. `App::make` returns the same instance. – Raja Khoury Nov 05 '19 at 03:01
  • @RajaKhoury Got it, it's a singleton then. Thanks. – Brand Guy Feb 01 '20 at 00:35
  • @BrandGuy I guess my comment is not entirely correct. Look here [make method](https://github.com/laravel/framework/blob/a06132cf639a91d106bab0b8693debfa0a42d815/src/Illuminate/Container/Container.php#L627) . This method resolves the class from the container and it doesn't imply it is a singleton. See here [docs](https://laravel.com/docs/master/container#binding) you can do a normal binding ( return a new instance every time) or a singleton binding ( return the same instance) and both can be resolved using `App::make` or `resolve(..)` – Raja Khoury Feb 02 '20 at 17:50
9

The good people in the Laravel forum answered this one for me http://laravel.io/forum/02-10-2014-laravel-4-confused-about-how-to-use-appmake

Pretty much if you can bind custom instantiation code with App::bind(); like so

App::bind('My\NewClass', function() use ($classArgs) {
    return new My\NewClass($classArgs['id'], $classArgs['title']);
});

// get the binding
$newClass = App::make('My\NewClass');
Theo Kouzelis
  • 3,195
  • 5
  • 37
  • 66