这在PHP中称为什么

In a code like the following may be found:

return View::make('hello')->with('name', $name);

What I know is that:

  • View is a class
  • make is one method of the class View
  • 'hello' is a parameter passed to the method make

What I don't know is: with does it a method of method?! does it a PHP keyword? or does it something defined (if it is, what is its definition?) in the make method?

class View() {
  protected $name;
  public function __construct($name){$this->name = $name;}
  public function with($s, $p){return $this;}
  public static function make($name){
    return new self($name);
  }
}

make - static method of a class
with - method of View object

Look at View::make('hello')->with('name', $name); as at followed:

$view = View::make('hello');
$view->with('name', $name);

return $thisin with method allows us to followed:

View::make('hello')->with('name1', $name1)
                   ->with('name2', $name2)
                   ->with('name3', $name3);

This pattern named chaining

As per the documentation the with method is used for ViewComposers, bounding the variables passed in the with method to the view for being displayed

This is method chaining. The with is just a function of the View class too. Chainable class functions return a refference to the class itself, so you can call other methods in the same line. So instead of creating a variable for the class instance and calling each function in a new line you can do this:

View::make('view')
    ->with('id', $id)
    ->with('title', $title)
    ->with('name', $name);

Without method chaining it would be like this:

$view = View::make('view');
$view->with('id', $id);
$view->with('title', $title);
$view->with('name', $name);

Also when you do View::make you actually call the make function of the View class without instantiation of the class, so you don't have to do it like this:

$view = new View();
$view->make('view');

However you still end up with a class instantiation because make() creates one, but our goal is to do things easy. We want to create a view with the least amount of readable code by throwing away obvious lines that just increasing noise in code but we had to write them due to the nature of PHP. This philosophy makes Laravel so beautifull.