What is the difference between laravel method vs trait vs facade [closed]

霸气de小男生 提交于 2019-12-12 09:00:20

问题


In a nutshell what would be the easiest way to compare the three?

method vs trait vs facade

Cheers!


回答1:


They don't really compare because they're really different things.

A method is a function that belongs to a class.

class MyClass
{
     public function this_is_a_method() { }
}

A trait is a means of sharing code between classes. A trait cannot be instantiated, but rather is included into another class. Both classes and traits can define methods.

trait MyTrait
{
     public function this_is_a_method() { }
}

Now that I have this trait I can update MyClass to use this trait.

class MyClass
{
     use MyTrait;
}

You can think of traits as copy and paste. Now MyClass copies the methods defined in MyTrait so you can do this.

$class = new MyClass();
$class->this_is_a_method();

Both methods and traits are features of PHP. Facades are a feature of Laravel. Facades are simply syntactic sugar to help get services out of the container.



来源:https://stackoverflow.com/questions/43357875/what-is-the-difference-between-laravel-method-vs-trait-vs-facade

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!