1

I decided to look on how artisan is initialized in Laravel 4.2 and saw function make($app)

public static function make($app)
    {
    $app->boot();

    $console = with($console = new static('Laravel Framework', $app::VERSION))
                            ->setLaravel($app)
                            ->setExceptionHandler($app['exception'])
                            ->setAutoExit(false);

    $app->instance('artisan', $console);

    return $console;
}

I am not really newbie in PHP but not master either... And I cannot understand wholly the meaning of (from PHP point of view)

$console = with($console = new static('Laravel Framework', $app::VERSION))
                        ->setLaravel($app)
                        ->setExceptionHandler($app['exception'])
                        ->setAutoExit(false);

Here is the link to source https://github.com/laravel/framework/blob/4.2/src/Illuminate/Console/Application.php

Thelambofgoat
  • 609
  • 1
  • 11
  • 24

2 Answers2

2

with method is a helper method (Laravel helper methods) which allows you to method chain. Es specially in PHP 5.3.X environment

From the documentation

$value = with(new Foo)->doWork();

This would create a new instance off the class Foo and call the method do on Foo

From the source

if ( ! function_exists('with'))
{
    /**
     * Return the given object. Useful for chaining.
     *
     * @param  mixed  $object
     * @return mixed
     */
    function with($object)
    {
        return $object;
    }
}

When you provide it an instance of an object -> it will return this instance. Now you are able to call methods or properties on this object.

this would be the same as doing the following:

 $foo = new Foo();
 $value = $foo->doWork();
DouglasDC3
  • 495
  • 2
  • 12
1

From the code comments it:

Create a new Console application.

I imagine your asking more about how.

  1. It calls the class constructor to create a new instance. Review the symfony class for more details on the constructor.
  2. Using with() allows chaining calls the additional methods setLaravel(), setExceptionHandler(), etc which finalize setting up the Laravel Application instance.

To learn more about new static review this question: New self vs. new static.

Community
  • 1
  • 1
Jason McCreary
  • 71,546
  • 23
  • 135
  • 174