{"id":18952774,"url":"https://github.com/sujon-ahmed/practice-php-oop","last_synced_at":"2025-10-11T05:38:44.142Z","repository":{"id":109467718,"uuid":"404233068","full_name":"Sujon-Ahmed/practice-php-oop","owner":"Sujon-Ahmed","description":"This repository will cover PHP OOP","archived":false,"fork":false,"pushed_at":"2024-03-13T09:07:43.000Z","size":70,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-01T02:41:46.877Z","etag":null,"topics":["oop","php7"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Sujon-Ahmed.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2021-09-08T06:16:44.000Z","updated_at":"2023-02-09T04:43:20.000Z","dependencies_parsed_at":null,"dependency_job_id":"f44d1e26-ad38-44f3-b041-a1c8073228fe","html_url":"https://github.com/Sujon-Ahmed/practice-php-oop","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sujon-Ahmed%2Fpractice-php-oop","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sujon-Ahmed%2Fpractice-php-oop/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sujon-Ahmed%2Fpractice-php-oop/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sujon-Ahmed%2Fpractice-php-oop/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Sujon-Ahmed","download_url":"https://codeload.github.com/Sujon-Ahmed/practice-php-oop/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239952598,"owners_count":19723922,"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":["oop","php7"],"created_at":"2024-11-08T13:34:36.113Z","updated_at":"2025-10-11T05:38:39.103Z","avatar_url":"https://github.com/Sujon-Ahmed.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"## PHP What is OOP?\n\nOOP stands for Object-Oriented Programming.\n\nProcedural programming is about writing procedures or functions that perform operations on the data, while object-oriented programming is about creating objects that contain both data and functions.\n\nObject-oriented programming has several advantages over procedural programming:\n\n- OOP is faster and easier to execute\n- OOP provides a clear structure for the programs\n- OOP helps to keep the PHP code DRY \"Don't Repeat Yourself\", and makes the code easier to maintain, modify and debug\n- OOP makes it possible to create full reusable applications with less code and shorter development time\n\n**Tip:** The \"Don't Repeat Yourself\" (DRY) principle is about reducing the repetition of code. You should extract out the codes that are common for the application, and place them at a single place and reuse them instead of repeating it.\n\n## PHP - What are Classes and Objects?\n\nClasses and objects are the two main aspects of object-oriented programming.\n\nLook at the following illustration to see the difference between class and objects:\n\n![oop class-object](images/oop-example.png)\n\nSo, a class is a template for objects, and an object is an instance of a class.\n\nWhen the individual objects are created, they inherit all the properties and behaviors from the class, but each object will have different values for the properties.\n\nLook at the next chapters to learn more about OOP.\n\n## OOP Case\n\nLet's assume we have a class named Fruit. A Fruit can have properties like name, color, weight, etc. We can define variables like $name, $color, and $weight to hold the values of these properties.\n\nWhen the individual objects (apple, banana, etc.) are created, they inherit all the properties and behaviors from the class, but each object will have different values for the properties.\n\n## Define a Class\n\nA class is defined by using the `class` keyword, followed by the name of the class and a pair of curly braces ({}). All its properties and methods go inside the braces:\n\n```php\n\u003c?php\nclass Fruit {\n  // code goes here...\n}\n?\u003e\n```\n\nBelow we declare a class named Fruit consisting of two properties ($name and $color) and two methods set_name() and get_name() for setting and getting the $name property:\n\n```php\n\u003c?php\nclass Fruit {\n  // Properties\n  public $name;\n  public $color;\n\n  // Methods\n  function set_name($name) {\n    $this-\u003ename = $name;\n  }\n  function get_name() {\n    return $this-\u003ename;\n  }\n}\n?\u003e\n```\n\n****Note: In a class, variables are called properties and functions are called methods!****\n\n## PHP - The $this Keyword\n\nThe $this keyword refers to the current object, and is only available inside methods.\n\nLook at the following example:\n\n```php\n\u003c?php\nclass Fruit {\n  public $name;\n}\n$apple = new Fruit();\n?\u003e\n```\n\nSo, where can we change the value of the $name property? There are two ways:\n\n1. Inside the class (by adding a set_name() method and use $this):\n\n```php\n\u003c?php\nclass Fruit {\n  public $name;\n  function set_name($name) {\n    $this-\u003ename = $name;\n  }\n}\n$apple = new Fruit();\n$apple-\u003eset_name(\"Apple\");\n\necho $apple-\u003ename;\n?\u003e\n```\n\n2. Outside the class (by directly changing the property value):\n\n```php\n\u003c?php\nclass Fruit {\n  public $name;\n}\n$apple = new Fruit();\n$apple-\u003ename = \"Apple\";\n\necho $apple-\u003ename;\n?\u003e\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsujon-ahmed%2Fpractice-php-oop","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsujon-ahmed%2Fpractice-php-oop","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsujon-ahmed%2Fpractice-php-oop/lists"}