$this不在静态方法本身中使用,它在传递给该方法的闭包中使用.
From the Laravel manual:
The Closure is bound to the underlying command instance, so you have full access to all of the helper methods you would typically be able to access on a full command class.
所以这个上下文中的$this是一个Command实例.这是使用PHP的bindTo方法实现的,该方法允许您指定任何给定闭包的范围.
但这种方法并不仅限于Artisan命令.一般来说,我们将此功能称为Facades:
Facades provide a “static” interface to classes that are available in the application’s 07003. Laravel ships with many facades which provide access to almost all of Laravel’s features. Laravel facades serve as “static proxies” to underlying classes in the service container, providing the benefit of a terse, expressive syntax while maintaining more testability and flexibility than traditional static methods.
还有一些其他外观,它们都提供对服务容器内的实例的静态访问.一些更常见的外观和方法是:
> Cache :: get(‘key’)和Cache :: set(‘key’,’value’)> Request :: input(‘some_field’)和Request :: only(‘some_field’)> Log :: info(‘要知道这个……’)> ……