问题描述
简而言之,比较这三个最简单的方法是什么?
In a nutshell what would be the easiest way to compare the three?
方法vs特质vs门面
干杯!
推荐答案
它们并没有真正的比较,因为它们是完全不同的东西.
They don't really compare because they're really different things.
方法是属于类的函数.
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() { }
}
现在我有了这个特征,我可以更新MyClass
来使用这个特征.
Now that I have this trait I can update MyClass
to use this trait.
class MyClass
{
use MyTrait;
}
您可以将特征视为复制和粘贴.现在MyClass
复制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();
方法和特征都是PHP的功能.立面是Laravel的功能.外墙只是语法糖,可以帮助您从容器中获取服务.
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.
这篇关于laravel方法与特征与立面之间有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!