Every developer should follow the PHP Standards Recommendations
1.1 Variables:
1.2 Functions and Methods:
1.3 Classes:
1.4 Constants:
1.5 Interfaces:
1.7 Namespaces:
|
2.1 Dependency Injection (DI):
2.2 New Code Placement:
2.3 SOLID Principles:
2.4 Unit Tests:
2.5 Encapsulation Over Inheritance:
Problem with Inheritance:Using inheritance might seem natural when trying to extend functionality, but it can lead to tightly coupled designs and rigid hierarchies.
Issues:
Better with Encapsulation:Encapsulation allows us to compose functionality dynamically and avoid the pitfalls of deep inheritance.
Advantages of Encapsulation Over Inheritance:
This approach adheres to the Open/Closed Principle (code is open for extension but closed for modification) and results in more maintainable, testable, and decoupled code. 2.6 Interface Usage:
2.7 Small, Focused Methods:
2.8 Avoid Hardcoding:
2.9 Readable Code:
2.10 Avoid Side Effects:
|
3.1 Avoid using framework base classes and services directly. And for sure Zend 1 is a very outdated framework and we definitely need to migrate to something newer. - Do not use ZfExtended_Factory except for models. Instead use $xyz = new NeededClass(); - Do not use Zend_Http_Client, use Guzzle and rely on PSR HTTP message interfaces TBA 3.2 Operations (Use cases) The Operations approach focuses on creating small, single-purpose classes, each dedicated to performing a specific task within the application. Instead of bundling multiple related actions (e.g., create, update, delete) into a single service class, each action is encapsulated in its own class, such as This approach, often referred to as the Use Cases pattern in some repositories, aligns with the principle of Single Responsibility and ensures that each class has one clearly defined purpose. Advantages:
Operation classes should be placed in a folder of a feature where they are applicable e.g. operations that are responsible for working with user in src/User/Operation, operations for language resource in src/LanguageResource/Operation, however more specific operation should be placed in feature folder e.g. src/LanguageResource/TaskTm/Operation 3.3 Repository Classes for Data Access In our application, the current use of Active Record ORM involves placing data access and persistence logic directly within model classes. To improve separation of concerns and maintainability, we should adopt the repository pattern. Repository classes act as a dedicated layer for handling all database interactions, encapsulating queries and persistence logic. This approach ensures that model classes focus solely on representing business entities, while repositories centralize data access, making it easier to manage, test, and reuse. For instance, instead of embedding custom query methods within the Repositories should be placed in src/Repository folder. But there might be a specific repository methods needed in particular plugin code or in particular features - then it is better to create a separate repo and place it to the specific place (feature of plugin). |