{"id":26452120,"url":"https://github.com/akrambl01/php-oop-guide","last_synced_at":"2025-03-18T17:26:23.318Z","repository":{"id":281710045,"uuid":"946166664","full_name":"Akrambl01/php-oop-guide","owner":"Akrambl01","description":"A guide to PHP Object-Oriented Programming (OOP), covering key concepts and best practices with examples.","archived":false,"fork":false,"pushed_at":"2025-03-10T18:09:25.000Z","size":70,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-10T19:24:05.132Z","etag":null,"topics":["codingtips","guide","oop","php","programming","webdevelopment"],"latest_commit_sha":null,"homepage":"","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/Akrambl01.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,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-03-10T18:02:35.000Z","updated_at":"2025-03-10T18:16:17.000Z","dependencies_parsed_at":"2025-03-10T19:34:12.109Z","dependency_job_id":null,"html_url":"https://github.com/Akrambl01/php-oop-guide","commit_stats":null,"previous_names":["akrambl01/php-oop-guide"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Akrambl01%2Fphp-oop-guide","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Akrambl01%2Fphp-oop-guide/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Akrambl01%2Fphp-oop-guide/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Akrambl01%2Fphp-oop-guide/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Akrambl01","download_url":"https://codeload.github.com/Akrambl01/php-oop-guide/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244267809,"owners_count":20425899,"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":["codingtips","guide","oop","php","programming","webdevelopment"],"created_at":"2025-03-18T17:26:22.530Z","updated_at":"2025-03-18T17:26:23.311Z","avatar_url":"https://github.com/Akrambl01.png","language":null,"readme":"# php-oop-guide\n![PHP OOP](https://github.com/AkramBl01/php-oop-guide/blob/main/OOPConcepts.png)\n## 📌 OOP in PHP\n\nWelcome to the **Object-Oriented Programming (OOP) in PHP** guide! This document covers fundamental OOP principles with practical examples in PHP. Whether you're a beginner or looking to refresh your knowledge, this guide will help you master OOP concepts efficiently.\n\n---\n\n## 🚀 Key OOP Concepts in PHP\n\n### 1️⃣ Classes and Objects\n\nA **class** is a blueprint for creating objects. **Objects** are instances of classes that contain properties (variables) and methods (functions).\n\n```php\nclass User {\n  private $name;\n  public $email;\n  public $password;\n\n  public function __construct($name, $email, $password) {\n    $this-\u003ename = $name;\n    $this-\u003eemail = $email;\n    $this-\u003epassword = $password;\n  }\n\n  function getName() {\n    return $this-\u003ename;\n  }\n\n  function login() {\n    return \"User $this-\u003ename is logged in.\";\n  }\n}\n\n$user1 = new User('John', 'john@gmail.com', '123456');\necho $user1-\u003egetName(); // Outputs: John\necho $user1-\u003elogin(); // Outputs: User John is logged in.\n```\n\n---\n\n### 2️⃣ Inheritance\n\nInheritance allows a class (child class) to inherit properties and methods from another class (parent class), promoting code reuse.\n\n```php\nclass Employee extends User {\n  public $title;\n\n  public function __construct($name, $email, $password, $title) {\n    parent::__construct($name, $email, $password);\n    $this-\u003etitle = $title;\n  }\n\n  public function getTitle() {\n    return $this-\u003etitle;\n  }\n}\n\n$employee1 = new Employee('Alice', 'alice@gmail.com', '123456', 'Manager');\necho $employee1-\u003egetTitle(); // Outputs: Manager\n```\n\n---\n\n### 3️⃣ The `final` Keyword\n\n- Prevents a class from being inherited.\n- Prevents methods from being overridden in child classes.\n\n```php\nfinal class FinalClass {\n  // This class cannot be extended\n}\n```\n\n---\n\n### 4️⃣ Encapsulation\n\nEncapsulation restricts direct access to object properties and ensures controlled data modification via methods.\n\n```php\nclass User {\n  private $name;\n  private $email;\n  private $password;\n\n  public function __construct($name, $email, $password) {\n    $this-\u003ename = $name;\n    $this-\u003eemail = $email;\n    $this-\u003epassword = $password;\n  }\n\n  public function getName() {\n    return $this-\u003ename;\n  }\n}\n```\n\n---\n\n### 5️⃣ Abstract Classes and Methods\n\nAbstract classes contain at least one abstract method and cannot be instantiated directly.\n\n```php\nabstract class AbstractUser {\n  protected $name;\n  protected $email;\n  protected $password;\n\n  public function __construct($name, $email, $password) {\n    $this-\u003ename = $name;\n    $this-\u003eemail = $email;\n    $this-\u003epassword = $password;\n  }\n\n  abstract public function login();\n}\n\nclass User extends AbstractUser {\n  public function login() {\n    return \"User $this-\u003ename is logged in.\";\n  }\n}\n```\n\n---\n\n### 6️⃣ Interfaces\n\nInterfaces define methods that must be implemented by a class.\n\n```php\ninterface Logger {\n  public function log($message);\n  public function error($message);\n}\n\nclass FileLogger implements Logger {\n  public function log($message) {\n    echo \"Log: $message\";\n  }\n\n  public function error($message) {\n    echo \"Error: $message\";\n  }\n}\n```\n\n---\n\n### 7️⃣ Magic Methods\n\nPHP provides predefined methods starting with `__` (double underscore) that perform special operations.\n\n```php\nclass Iphone {\n  public function __call($method, $params) {\n    echo \"Method $method not found.\";\n  }\n}\n\n$phone = new Iphone();\n$phone-\u003esayHello(); // Outputs: Method sayHello not found.\n```\n\n---\n\n### 8️⃣ Cloning Objects\n\nCloning creates a copy of an object without affecting the original.\n\n```php\nclass Phone {\n  public $name;\n  public function __construct($name) {\n    $this-\u003ename = $name;\n  }\n}\n\n$phone1 = new Phone('iPhone');\n$phone2 = clone $phone1;\n$phone2-\u003ename = 'Samsung';\n\necho $phone1-\u003ename; // Outputs: iPhone\n```\n\n---\n\n### 9️⃣ Static Properties and Methods\n\nStatic methods and properties belong to a class rather than an instance.\n\n```php\nclass MyClass {\n  public static $counter = 0;\n\n  public function __construct() {\n    self::$counter++;\n  }\n}\n\necho MyClass::$counter; // Outputs: 0\n$obj1 = new MyClass();\n$obj2 = new MyClass();\necho MyClass::$counter; // Outputs: 2\n```\n\n---\n\n### 🔟 Method Chaining\n\nMethod chaining allows multiple method calls on the same object.\n\n```php\nclass MyClass {\n  public function sayHi() {\n    echo \"Hi user.\u003cbr\u003e\";\n    return $this;\n  }\n\n  public function sayHello() {\n    echo \"Hello user.\u003cbr\u003e\";\n    return $this;\n  }\n}\n\n$obj = new MyClass();\n$obj-\u003esayHi()-\u003esayHello();\n```\n\n---\n\n## 📢 Conclusion\n\nOOP in PHP provides a structured approach to coding. Mastering these concepts will help you write **reusable, maintainable, and efficient** code.\n\n🔹 **Happy Coding!** 🚀\n\n---\n\n\n## 📜 License\n\nThis project is open-source and available under the [MIT License](LICENSE).\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fakrambl01%2Fphp-oop-guide","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fakrambl01%2Fphp-oop-guide","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fakrambl01%2Fphp-oop-guide/lists"}