Recent Posts
Archives

Posts Tagged ‘SOLID’

PostHeaderIcon [PHPForumParis2023] Open/Closed Principle: Extend, Don’t Extends! – Thomas Dutrion

Thomas Dutrion, CTO and a passionate advocate for clean code, presented an engaging session at Forum PHP 2023 on the Open/Closed Principle, a cornerstone of the SOLID principles. With a playful nod to avoiding PHP’s extends keyword, Thomas clarified how to design extensible systems without relying on inheritance. His talk, infused with practical examples and a call for team collaboration, offered PHP developers a clear framework for building flexible, maintainable codebases.

Demystifying the Open/Closed Principle

Thomas began by explaining the Open/Closed Principle, which states that software entities should be open for extension but closed for modification. He emphasized that this principle enables developers to add new functionality without altering existing code, reducing the risk of introducing bugs. Using relatable analogies, Thomas distinguished between “extending” a system’s behavior through design patterns and the pitfalls of using PHP’s extends for inheritance, which can lead to rigid, tightly coupled code.

Practical Techniques for Extension

Delving into implementation, Thomas showcased techniques like decorators and callbacks to achieve extensibility. He provided examples of middleware patterns, such as those defined in PSR-15, where request handlers are passed through a stack of processes, allowing behavior to be extended dynamically. Thomas cautioned against overly complex callback chains, advocating for clear, team-aligned designs. His demonstrations highlighted how these patterns maintain code stability while allowing for seamless enhancements.

Team Collaboration and Clarity

Concluding his talk, Thomas stressed the importance of team agreement when applying the Open/Closed Principle. He noted that patterns like decorators often rely on dependency injection, which can obscure implementation details unless well-documented. By advocating for clear communication and tools like event dispatchers, Thomas inspired developers to work collaboratively, ensuring extensible designs are both effective and understandable within their teams.