In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
What this article shares with you is the contract about how to analyze the underlying principles of Laravel. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
Brief introduction
A contract in Laravel refers to a series of interfaces (interface) that the framework provides to define core services.
For example, the Illuminate\ Contracts\ Queue\ Queue contract defines the methods required to queue tasks, while the Illuminate\ Contracts\ Mail\ Mailer contract defines the methods required to send e-mail messages.
The framework provides a corresponding implementation for each contract. For example, Laravel provides queue implementations with various drivers and mail-driven implementations supported by SwiftMailer.
All Laravel contracts have their own GitHub libraries. This provides a quick reference guide for all available contracts and can also be used separately as a low-coupling extension package for other package developers.
Contract VS. Facades
Laravel Facades and helper functions provide an easy way to use Laravel services without having to parse the contract from the service container through the type hint. In most cases, each Facades has an equivalent contract.
Unlike facade, which does not require type hints in the constructor, contracts allow you to define explicit dependencies in classes. Some developers tend to clearly define their dependencies in the form of contracts, while others prefer the convenience of Facades.
For most applications, either a facade or a contract is fine. However, if you are building an extension package, you should strongly consider contracts in order to facilitate testing.
When to use the contract
To sum up, the use of contracts or Facades largely comes down to the preferences of the individual or the development team. Both contracts and Facades can create robust, easy-to-test Laravel applications. If you focus on the single responsibility of a class for a long time, you will notice that there is not much practical difference between using a contract or a Facades.
However, you may still have a few questions about the contract. For example, why use interfaces? Is it more complicated not to use an interface? Let's talk about the reasons for using interfaces: low coupling and simplicity.
Low coupling
First, let's look at some code for a highly coupled cache implementation. It is as follows:
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.