What's the difference between a Contract in Laravel and an Interface in PHP?

五迷三道 提交于 2019-12-10 02:22:00

问题


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.

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?

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


回答1:


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.

You can read more on this subject in the documentation http://laravel.com/docs/5.1/contracts




回答2:


"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.

So basically, "contract" isn't a new naming convention. It's a common term that Taylor is using.




回答3:


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.

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.



来源:https://stackoverflow.com/questions/34072046/whats-the-difference-between-a-contract-in-laravel-and-an-interface-in-php

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