Skip to content

Instantly share code, notes, and snippets.

@DevWael
Created May 25, 2023 03:59
Show Gist options
  • Save DevWael/4b852e0e996d4171a95c13d1cedb70c0 to your computer and use it in GitHub Desktop.
Save DevWael/4b852e0e996d4171a95c13d1cedb70c0 to your computer and use it in GitHub Desktop.
PHP design principles, patterns, and standards

Here are some design principles, patterns, and standards that you should consider when developing software:

Design Principles

  • Single responsibility principle: A class should have only one responsibility. This makes the class easier to understand, test, and maintain.
  • Open/closed principle: Classes should be open for extension but closed for modification. This means that you should be able to add new features to a class without having to modify the existing code.
  • Liskov substitution principle: Any subclass should be substitutable for its superclass. This means that you should be able to use a subclass in place of its superclass without having to change the code that uses it.
  • Interface segregation principle: Clients should not be forced to depend on methods they do not use. This means that you should not expose methods in an interface that are not needed by all clients.
  • Dependency inversion principle: A.K.A SOLID principle. Depend upon abstractions, not concretions. This means that you should design your code so that it depends on interfaces and abstract classes, not on concrete classes.

Design Patterns

  • Singleton: The singleton pattern ensures that there is only one instance of a class that implements it. This can be useful for classes that are responsible for loading a plugin or application, as you don't want them to run twice.
  • Factory: The factory pattern is used to create objects without having to specify their concrete type. This can be useful for creating objects that are dynamically loaded or for creating objects that are interchangeable.
  • Strategy: The strategy pattern allows you to change the behavior of an object at runtime. This can be useful for objects that need to be able to handle different types of data or for objects that need to be able to adapt to different situations.
  • Observer: The observer pattern allows you to subscribe to events that are emitted by an object. This can be useful for objects that need to be notified of changes in the state of other objects or for objects that need to be able to react to events in the environment.
  • Facade: The facade pattern provides a simplified interface to a complex system. This can be useful for making a system easier to use or for hiding the complexity of a system from users.
  • Adapter: The adapter pattern allows you to make two incompatible interfaces compatible. This can be useful for making old code compatible with new code or for making code from different libraries compatible with each other.
  • Decorator: The decorator pattern allows you to add new functionality to an object without having to modify the object itself. This can be useful for adding new features to an object or for changing the behavior of an object without having to rewrite the object.
  • Proxy: The proxy pattern provides a substitute object for an expensive or unavailable object. This can be useful for caching objects, for accessing objects that are on a remote server, or for accessing objects that are protected by security.
  • Command: The command pattern encapsulates a request as an object. This can be useful for decoupling the caller from the callee, for logging requests, or for queuing requests.
  • Iterator: The iterator pattern allows you to iterate over a collection without having to know the underlying implementation of the collection. This can be useful for making code more portable or for making code more efficient.
  • Visitor: The visitor pattern allows you to apply a single operation to all the objects in a collection. This can be useful for performing bulk operations on a collection or for performing operations that are not supported by the collection itself.

Standards

  • PHP Standards Recommendations (PSRs): The PSRs are a set of guidelines for developing high-quality PHP code. They cover a wide range of topics, including coding style, error handling, and testing.
  • WordPress Coding Standards (WordPress Coding Standards): The WordPress Coding Standards are a set of guidelines for developing high-quality WordPress code. They cover a wide range of topics, including coding style, error handling, and testing.

It is important to note that these are just a few of the many design principles, patterns, and standards that are available. There is no one-size-fits-all solution, and the best approach will vary depending on the specific project. It is important to learn about as many principles, patterns, and standards as possible so that you can choose the right ones for your specific needs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment