{"id":24201646,"url":"https://github.com/ramazancetinkaya/solid-principles-for-php","last_synced_at":"2025-10-24T10:01:48.777Z","repository":{"id":218752358,"uuid":"747222963","full_name":"ramazancetinkaya/solid-principles-for-php","owner":"ramazancetinkaya","description":"In software engineering, SOLID is a mnemonic acronym for five design principles intended to make object-oriented designs more understandable, flexible, and maintainable.","archived":false,"fork":false,"pushed_at":"2024-01-23T15:41:02.000Z","size":5,"stargazers_count":11,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-13T21:16:32.400Z","etag":null,"topics":["solid","solid-ilkeleri","solid-prensipleri","solid-principles","solid-principles-examples"],"latest_commit_sha":null,"homepage":"https://en.wikipedia.org/wiki/SOLID","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ramazancetinkaya.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2024-01-23T14:11:17.000Z","updated_at":"2024-12-01T19:58:25.000Z","dependencies_parsed_at":"2024-01-23T16:57:44.764Z","dependency_job_id":null,"html_url":"https://github.com/ramazancetinkaya/solid-principles-for-php","commit_stats":null,"previous_names":["ramazancetinkaya/solid-principles-for-php"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ramazancetinkaya%2Fsolid-principles-for-php","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ramazancetinkaya%2Fsolid-principles-for-php/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ramazancetinkaya%2Fsolid-principles-for-php/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ramazancetinkaya%2Fsolid-principles-for-php/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ramazancetinkaya","download_url":"https://codeload.github.com/ramazancetinkaya/solid-principles-for-php/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241645057,"owners_count":19996299,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["solid","solid-ilkeleri","solid-prensipleri","solid-principles","solid-principles-examples"],"created_at":"2025-01-13T21:16:36.668Z","updated_at":"2025-10-24T10:01:48.715Z","avatar_url":"https://github.com/ramazancetinkaya.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003ch1 align=\"center\"\u003e\n  SOLID Principles in PHP\n\u003c/h1\u003e\n\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://github.com/ramazancetinkaya/solid-principles-for-php\"\u003e\n    \u003cimg src=\"https://rijsat.com/wp-content/uploads/2023/11/image-3.png\" alt=\"Logo\"\u003e\n  \u003c/a\u003e\n\n  \u003cp align=\"center\"\u003e\n    In software engineering, SOLID is a mnemonic acronym for five design principles intended to make object-oriented designs more understandable, flexible, and maintainable.\n  \u003c/p\u003e\n\u003c/p\u003e\n\n## Introduction\n\nSOLID principles are a set of five design principles that aim to make software design more understandable, flexible, and maintainable. These principles were introduced by Robert C. Martin and have become fundamental guidelines for object-oriented design. In this guide, we'll explore each SOLID principle and demonstrate how to apply them in PHP with clear and detailed examples.\n\n## SOLID Principles\n\n* Single Responsibility Principle (SRP)\n* Open/Closed Principle (OCP)\n* Liskov Substitution Principle (LSP)\n* Interface Segregation Principle (ISP)\n* Dependency Inversion Principle (DIP)\n\n### SOLID Principles Overview\n\n1) **Single Responsibility Principle (SRP):**\n\n    A class should have only one reason to change, meaning it should have only one responsibility.\n\n2) **Open/Closed Principle (OCP):**\n\n    Software entities (classes, modules, functions, etc.) should be open for extension but closed for modification.\n\n3) **Liskov Substitution Principle (LSP):**\n\n    Subtypes must be substitutable for their base types without altering the correctness of the program.\n\n4) **Interface Segregation Principle (ISP):**\n\n    Clients should not be forced to depend on interfaces they do not use.\n\n5) **Dependency Inversion Principle (DIP):**\n\n    High-level modules should not depend on low-level modules; both should depend on abstractions. Abstractions should not depend on details; details should depend on abstractions.\n\n## Single Responsibility Principle (SRP) in PHP\n\nThe Single Responsibility Principle suggests that a class should only have one reason to change.\n\nLet's consider a simple example:\n\n```php\n// Before SRP\n\nclass Report {\n    public function generate() {\n        // Code to generate report\n    }\n\n    public function saveToFile() {\n        // Code to save report to a file\n    }\n}\n```\n\nIn this example, the Report class has both report generation and file-saving responsibilities. Let's refactor this to adhere to SRP:\n\n```php\n// After SRP\n\nclass Report {\n    public function generate() {\n        // Code to generate report\n    }\n}\n\nclass ReportSaver {\n    public function saveToFile(Report $report) {\n        // Code to save report to a file\n    }\n}\n```\n\nNow, we have two classes, each with a single responsibility: Report for generating reports and ReportSaver for saving reports to a file.\n\n## Open/Closed Principle (OCP) in PHP\n\nThe Open/Closed Principle encourages entities to be open for extension but closed for modification.\n\nLet's consider an example:\n\n```php\n// Before OCP\n\nclass Circle {\n    public function calculateArea($radius) {\n        return 3.14 * $radius * $radius;\n    }\n}\n```\n\nTo adhere to OCP, we can introduce an interface and create a class that extends it:\n\n```php\n// After OCP\n\ninterface Shape {\n    public function calculateArea();\n}\n\nclass Circle implements Shape {\n    private $radius;\n\n    public function __construct($radius) {\n        $this-\u003eradius = $radius;\n    }\n\n    public function calculateArea() {\n        return 3.14 * $this-\u003eradius * $this-\u003eradius;\n    }\n}\n```\n\nNow, if we want to add more shapes, we can create new classes implementing the Shape interface without modifying the existing code.\n\n## Liskov Substitution Principle (LSP) in PHP\n\nThe Liskov Substitution Principle ensures that objects of a base class can be replaced with objects of a derived class without affecting the correctness of the program.\n\nConsider the following example:\n\n```php\n// Before LSP\n\nclass Bird {\n    public function fly() {\n        // Code for flying\n    }\n}\n\nclass Penguin extends Bird {\n    public function fly() {\n        // Penguins can't fly, but this method is inherited\n    }\n}\n```\n\nTo adhere to LSP, we can create a separate interface:\n\n```php\n// After LSP\n\ninterface Flyable {\n    public function fly();\n}\n\nclass Bird implements Flyable {\n    public function fly() {\n        // Code for flying\n    }\n}\n\nclass Penguin implements Flyable {\n    public function fly() {\n        // Penguins can't fly, but this method is overridden\n        throw new Exception(\"Penguins can't fly\");\n    }\n}\n```\n\nNow, both Bird and Penguin implement the Flyable interface, ensuring correct substitution.\n\n## Interface Segregation Principle (ISP) in PHP\n\nThe Interface Segregation Principle suggests that a class should not be forced to depend on interfaces it does not use.\n\nConsider the following example:\n\n```php\n// Before ISP\n\ninterface Worker {\n    public function work();\n    public function eat();\n}\n```\n\nIf a class doesn't need the eat method, it is forced to implement it. Let's refactor using ISP:\n\n```php\n// After ISP\n\ninterface Workable {\n    public function work();\n}\n\ninterface Eatable {\n    public function eat();\n}\n\nclass Robot implements Workable {\n    public function work() {\n        // Code for working\n    }\n}\n\nclass Human implements Workable, Eatable {\n    public function work() {\n        // Code for working\n    }\n\n    public function eat() {\n        // Code for eating\n    }\n}\n```\n\nNow, classes can implement only the interfaces they need.\n\n## Dependency Inversion Principle (DIP) in PHP\n\nThe Dependency Inversion Principle states that high-level modules should not depend on low-level modules.\n\nLet's consider the following example:\n\n```php\n// Before DIP\n\nclass LightBulb {\n    public function turnOn() {\n        // Code to turn on the light bulb\n    }\n}\n\nclass Switch {\n    private $bulb;\n\n    public function __construct(LightBulb $bulb) {\n        $this-\u003ebulb = $bulb;\n    }\n\n    public function operate() {\n        // Code to operate the switch\n        $this-\u003ebulb-\u003eturnOn();\n    }\n}\n```\n\nTo adhere to DIP, we can introduce an interface:\n\n```php\n// After DIP\n\ninterface Switchable {\n    public function turnOn();\n}\n\nclass LightBulb implements Switchable {\n    public function turnOn() {\n        // Code to turn on the light bulb\n    }\n}\n\nclass Switch {\n    private $device;\n\n    public function __construct(Switchable $device) {\n        $this-\u003edevice = $device;\n    }\n\n    public function operate() {\n        // Code to operate the switch\n        $this-\u003edevice-\u003eturnOn();\n    }\n}\n```\n\nNow, Switch depends on the abstraction Switchable, allowing for greater flexibility in the future.\n\n## Conclusion\nIn this guide, we've explored the SOLID principles and demonstrated how to apply them in PHP through clear and practical examples.\n\nBy following these principles, you can create more maintainable, scalable, and robust software systems.\n\nAlways strive to write code that is not only functional but also follows sound design principles for long-term success in your projects.\n\n## Contact\n\nFor any inquiries or feedback, feel free to reach out to us via email.\n\n📧 Email: [ramazancetinkayadev@outlook.com](mailto:ramazancetinkayadev@outlook.com)\n\n## Credits\n\nThis guide was made possible by the following awesome contributors:\n\n- **Ramazan Çetinkaya** - [@ramazancetinkaya](https://github.com/ramazancetinkaya)\n  - Lead Developer\n\nSpecial thanks to the following resources:\n\n- [Wikipedia](https://en.wikipedia.org/wiki/SOLID)\n\nIf you've contributed to this guide and your name is not listed, please let us know, and we'll add you!\n\nThank you to everyone who has helped make this guide better!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Framazancetinkaya%2Fsolid-principles-for-php","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Framazancetinkaya%2Fsolid-principles-for-php","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Framazancetinkaya%2Fsolid-principles-for-php/lists"}