PHP的SOLID设计原则

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了PHP的SOLID设计原则脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

SOLID Design PRinciples,

这是一个比设计模式更高级别的概念,

以构建良好代码为目标,真正掌握了就是大师级别了。

我~~~仅知晓~

@H_404_14@
/*SOLID Design Principles
• S: Single responsibilITy principle (SRP)
• O: OPEn/closed principle (OCP)
• L: Liskov substitution principle (LSP)
• I: Interface Segregation Principle (ISP)
• D: Dependency inversion principle (DIP)

The single responsibility principle deals with classes that try to do too much.
The responsibility in this context refers to reason to change. As per the Robert C.
Martin deFinition:
"A class should have only one reason to change."

The open/closed principle states that a class should be open for extension but closed
for modification,as per the deFinition found on Wikipedia:
"Software entities (classes,modules,functions,etc.) should be open for extension,but closed for modification"
The open for extension part means that we should design our classes so that new
functionality can be added if needed. The closed for modification part means that
this new functionality should fit in without modifying the original class. The class
should only be modified in case of a bug fix,not for adding new functionality.

The Liskov substitution principle talks about inheritance. It specifies how we
should design our classes so that client dependencies can be replaced by subclasses
without the client seeing the difference,as per the deFinition found on Wikipedia:
"objects in a program should be replaceable with instances of their suBTypes
without altering the correctness of that program"
While there might be some specific functionality added to the subclass,it has to
conform to the same behavior as its base class. Otherwise the Liskov principle
is violated.
When it comes to PHP and sub-classing,we have to look beyond simple concrete
classes and differentiate: concrete class,abstract class,and interface. each of the three
can be put in the context of a base class,while everything extending or implementing
it can be looked at as a derived class.

The Interface Segregation Principle states that clients should only implement
interfaces they actually use. They should not be forced to implement interfaces
they do not use. As per the deFinition found on Wikipedia:
"many client-specific interfaces are better than one general-purpose interface"
What this means is that we should split large and fat interfaces into several small and
lighter ones,segregating it so that smaller interfaces are based on groups of methods,each serving one specific functionality.

The dependency inversion principle states that entities should depend on
abstractions and not on concretions. That is,a high level module should not
depend on a low level module,rather the abstraction. As per the deFinition
found on Wikipedia:
"One should depend upon abstractions. Do not depend upon concretions."
This principle is important as it plays a major role in decoupling our software.
*/

脚本宝典总结

以上是脚本宝典为你收集整理的PHP的SOLID设计原则全部内容,希望文章能够帮你解决PHP的SOLID设计原则所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。