Laravel 中的合约和 PHP 中的接口有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34072046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference between a Contract in Laravel and an Interface in PHP?
提问by Brynn Bateman
As far as I can tell, Laravel refers to the interfaces it extends as Contracts because they are used by Laravel. But this seems a bit like circular reasoning. There is no value added in changing the terminology of an existing PHP feature simply because your project uses it.
据我所知,Laravel 将它扩展的接口称为契约,因为它们被 Laravel 使用。但这似乎有点像循环推理。仅仅因为您的项目使用了现有的 PHP 特性,就改变它的术语并没有任何附加价值。
Is there something more to it? What's the logic behind coining a new term for something that's a standard PHP feature? Or is there some feature of Contracts that are not already in Interfaces?
还有更多的东西吗?为标准的 PHP 特性创造一个新术语背后的逻辑是什么?还是接口中还没有合同的某些功能?
Edit: To clarify, it's the usage of Contract as a proper noun in the documentation that has me confused, as explained in my comment on Thomas's post
编辑:澄清一下,正如我对Thomas 帖子的评论中所解释的,在文档中使用 Contract 作为专有名词让我感到困惑
回答by Thomas Kim
"Contract" isn't some new terminology that Taylor coined. It's a very common term programmers use.
“合同”不是泰勒创造的一些新术语。这是程序员使用的一个非常常见的术语。
An interface is a contract, but a contract doesn't necessarily have to be an interface. The interface in a nutshell defines the contract that the classes must implement.
接口是契约,但契约不一定是接口。简而言之,接口定义了类必须实现的契约。
An abstract class is also a contract. The difference is that an abstract class can provide actual implementations, state, etc., and as a result, it is (in a sense) a more rigorous contract.
抽象类也是契约。不同之处在于抽象类可以提供实际的实现、状态等,因此它(在某种意义上)是一个更严格的契约。
Another key difference is that a child class can only extend 1 abstract class but it can implement multiple interfaces.
另一个关键区别是子类只能扩展 1 个抽象类,但它可以实现多个接口。
So basically, "contract" isn't a new naming convention. It's a common term that Taylor is using.
所以基本上,“合同”不是一个新的命名约定。这是泰勒使用的常用术语。
回答by Gennady Basov
It's just a nice word to describe the idea of using interfaces.
这只是描述使用接口的想法的一个好词。
Laravel contracts are just PHP interfaces so they don't provide any other functionality.
Laravel 合约只是 PHP 接口,因此它们不提供任何其他功能。
You can read more on this subject in the documentation http://laravel.com/docs/5.1/contracts
您可以在文档http://laravel.com/docs/5.1/contracts 中阅读有关此主题的更多信息
回答by Fabio Antunes
As others have said, that is just a fancy word for Interfaces, but I think that Taylor made that decision to make it more personal.
正如其他人所说,这对 Interfaces 来说只是一个花哨的词,但我认为 Taylor 做出这个决定是为了让它更加个性化。
What I mean by personal is that interface it's a very broad/common word on programming language, you have your interfaces, libraries (that you might be using) have their own interfaces and so on.
我所说的个人的意思是接口它是编程语言中一个非常广泛/常用的词,你有你的接口,库(你可能正在使用)有自己的接口等等。
Contracts you just assume as the Laravel interfaces it's like a wrapper or alias for all the Interfaces that belong to this repo.
你只是假设为 Laravel 接口的合同就像属于这个 repo 的所有接口的包装器或别名。