{"id":210,"date":"2020-12-13T10:27:52","date_gmt":"2020-12-13T10:27:52","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/php\/?p=210"},"modified":"2026-10-29T01:26:31","modified_gmt":"2026-10-29T01:26:31","slug":"php-object-oriented-programming","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/php\/php-object-oriented-programming\/","title":{"rendered":"PHP &#8211; Object Oriented Programming &#8211; 1"},"content":{"rendered":"<p>In this chapter, we would cover Object Oriented Programming(OOP) concepts in PHP with examples.<\/p>\n<p>Object Oriented Programming is a way to write programs in terms of objects in the real world. So just like the world around us is made up of objects, we would also have our program made up of objects. And then these objects would be have some attributes and also be able to interact in some way.<\/p>\n<ol>\n<li><a href=\"#t1\">Object Oriented Programming Concepts<\/a><\/li>\n<li><a href=\"#t2\">Advanced Object Oriented Programming Concepts<\/a><\/li>\n<li><a href=\"#t3\">Defining a Class<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. Object Oriented Programming Concepts<\/strong><\/h4>\n<p>Before we would consider some examples, let&#8217;s first take a look at some OOP concepts.<\/p>\n<p><strong>Object<\/strong> &#8211; an object is simply what it is. Something, that has some attributes and behavior. For instance Vehicle(has wheels and speed), animal etc. Such objects can be defined in OOP as we would see later<\/p>\n<p><strong>Class<\/strong> &#8211; this is a blueprint for creating objects. A class is created in PHP as a programmer-defined data type. I would specify what the attributes(member variables) and behaviour(member functions) of it&#8217;s objects would be. So once you have a class, you can then use that class to create object by a process called instantiation. Therefore, we say an objects is an instance of a a class<\/p>\n<p><strong>Member Variable<\/strong> &#8211; these a variable defined in a class as attributes of the class. They are normally invisible outside the class and therefore accessible using the member functions<\/p>\n<p><strong>Member Functions<\/strong> &#8211; these are functions defined inside a class. They specify what operations the class can perform. They are also used to access the class member variables.<\/p>\n<p><strong>Inheritance<\/strong> &#8211; this is\u00a0 a way for a class to be created based on existing class. It then can inherit some member variables or functions from the parent class<\/p>\n<p><strong>Parent Class<\/strong> &#8211; This is a class whose variables or functions are inherited by another class. It is also called parent class, super class or base class<\/p>\n<p><strong>Child Class<\/strong> &#8211; this is a class that inherits from another class. It is also known as derived class or subclass<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. Advanced OOP Concepts<\/strong><\/h4>\n<p>Other advanced object oriented programming concepts are outlined below:<\/p>\n<p><strong>Polymorphism<\/strong> &#8211; this is a situation where a the same function can be used to perform different operations. For instance, two function may\u00a0 have the same name but the operation to perform would be based on the type and number of it&#8217;s parameters<\/p>\n<p><strong>Overloading<\/strong> &#8211; this is a type of polymorphism where things like operators or functions can be made to have different implementations<\/p>\n<p><strong>Abstraction<\/strong> &#8211; also data abstraction. This is\u00a0 a way to keep certain implementation details hidden from the users. It is related with <em>Information Hiding<\/em>.<\/p>\n<p><strong>Encapsulation<\/strong> &#8211; this is a concept of bundling(encapsulating) all the data and functions of an object together in a class.<\/p>\n<p><strong>Constructor<\/strong> &#8211; this is a special type of function that has the name of the class and is invoked automatically whenever the\u00a0 an object of the class is instantiated<\/p>\n<p><strong>Destructor<\/strong> &#8211; this is a special type of function that is called when objects goes out of scope.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. Defining a Class<\/strong><\/h4>\n<p>A class in PHP is defined using the <strong>class<\/strong> keyword, based on the following syntax:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #557799;\">&lt;?php<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">className<\/span> {\r\n        <span style=\"color: #008800; font-weight: bold;\">var<\/span> <span style=\"color: #996633;\">$memberVariable1<\/span>;\r\n        <span style=\"color: #008800; font-weight: bold;\">var<\/span> <span style=\"color: #996633;\">$memberVariable2<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"I am member variable\"<\/span>;\r\n\r\n        <span style=\"color: #008800; font-weight: bold;\">function<\/span> <span style=\"color: #0066bb; font-weight: bold;\">memberFunction<\/span>(<span style=\"color: #996633;\">$param1<\/span>, <span style=\"color: #996633;\">$param2<\/span>){\r\n            <span style=\"color: #888888;\">\/*<\/span>\r\n<span style=\"color: #888888;\">            function body<\/span>\r\n<span style=\"color: #888888;\">            *\/<\/span>\r\n        }\r\n        <span style=\"color: #888888;\">\/\/more codes<\/span>\r\n    }\r\n<span style=\"color: #557799;\">?&gt;<\/span>\r\n<\/pre>\n<p>Take note of what happens in the code:<\/p>\n<ul>\n<li>First you use the keyword class, followed by the name of the class<\/li>\n<li>The content of the class must be in curly braces<\/li>\n<li>Member variables are defined using the keyword var<\/li>\n<li>Member variables defined can also be assigned values at the point they are defined<\/li>\n<li>Functions definition begin with the keyword function, and just exactly like functions are defined as we covered under<a href=\"https:\/\/kindsonthegenius.com\/php\/php-functions\/\" target=\"_blank\" rel=\"noopener\"> PHP Functions<\/a><\/li>\n<\/ul>\n<p>Let&#8217;s take an example. Here, we define a class called Product<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #557799;\">&lt;?php<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Product<\/span> {\r\n        <span style=\"color: #888888;\">\/\/member variables<\/span>\r\n        <span style=\"color: #008800; font-weight: bold;\">var<\/span> <span style=\"color: #996633;\">$name<\/span>;\r\n        <span style=\"color: #008800; font-weight: bold;\">var<\/span> <span style=\"color: #996633;\">$description<\/span>;\r\n        <span style=\"color: #008800; font-weight: bold;\">var<\/span> <span style=\"color: #996633;\">$price<\/span>;\r\n        <span style=\"color: #008800; font-weight: bold;\">var<\/span> <span style=\"color: #996633;\">$stock<\/span>;\r\n\r\n        <span style=\"color: #888888;\">\/\/member function<\/span>\r\n        <span style=\"color: #008800; font-weight: bold;\">function<\/span> <span style=\"color: #0066bb; font-weight: bold;\">addStock<\/span>(<span style=\"color: #996633;\">$quantity<\/span>){\r\n            <span style=\"color: #996633;\">$this<\/span><span style=\"color: #333333;\">-&gt;<\/span><span style=\"color: #0000cc;\">stock<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"color: #996633;\">$stock<\/span> <span style=\"color: #333333;\">+<\/span> <span style=\"color: #996633;\">$quantity<\/span>;\r\n        }\r\n\r\n        <span style=\"color: #008800; font-weight: bold;\">function<\/span> <span style=\"color: #0066bb; font-weight: bold;\">getPrice<\/span>() {\r\n            <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #996633;\">$this<\/span><span style=\"color: #333333;\">-&gt;<\/span><span style=\"color: #0000cc;\">price<\/span>;\r\n        }\r\n\r\n        <span style=\"color: #008800; font-weight: bold;\">function<\/span> <span style=\"color: #0066bb; font-weight: bold;\">setPrice<\/span>(<span style=\"color: #996633;\">$newPrice<\/span>){\r\n            <span style=\"color: #996633;\">$this<\/span><span style=\"color: #333333;\">-&gt;<\/span><span style=\"color: #0000cc;\">price<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"color: #996633;\">$newPrice<\/span>;\r\n        }\r\n    }\r\n<span style=\"color: #557799;\">?&gt;<\/span>\r\n<\/pre>\n<p>The $this variable is a special PHP variable and it refers to the current object.<\/p>\n<p><strong>As an exercise for you<\/strong>, create classes for the following object: Car, Animal and Oven. Each of the classes should have two member variables and two member functions.<\/p>\n<p>In the next part, we would further explore Object Oriented Programming in PHP and see how we can create objects from classes.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this chapter, we would cover Object Oriented Programming(OOP) concepts in PHP with examples. Object Oriented Programming is a way to write programs in terms &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-210","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>PHP - Object Oriented Programming - 1 | Kindson The Genius<\/title>\n<meta name=\"description\" content=\"PHP - Object Oriented Programming - 1 \u2014 free PHP tutorial by Kindson Munonye with clear examples. Step-by-step for beginners. Part of Kindson The Genius.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/kindsonthegenius.com\/php\/php-object-oriented-programming\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PHP - Object Oriented Programming - 1 | Kindson The Genius\" \/>\n<meta property=\"og:description\" content=\"PHP - Object Oriented Programming - 1 \u2014 free PHP tutorial by Kindson Munonye with clear examples. Step-by-step for beginners. Part of Kindson The Genius.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/kindsonthegenius.com\/php\/php-object-oriented-programming\/\" \/>\n<meta property=\"og:site_name\" content=\"PHP Tutorial\" \/>\n<meta property=\"article:published_time\" content=\"2020-12-13T10:27:52+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-10-29T01:26:31+00:00\" \/>\n<meta name=\"author\" content=\"kindsonthegenius\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"kindsonthegenius\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/php-object-oriented-programming\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/php-object-oriented-programming\\\/\"},\"author\":{\"name\":\"kindsonthegenius\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/#\\\/schema\\\/person\\\/a2dcea4c76668e90e5842583fd5a5f60\"},\"headline\":\"PHP &#8211; Object Oriented Programming &#8211; 1\",\"datePublished\":\"2020-12-13T10:27:52+00:00\",\"dateModified\":\"2026-10-29T01:26:31+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/php-object-oriented-programming\\\/\"},\"wordCount\":691,\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/php-object-oriented-programming\\\/\",\"url\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/php-object-oriented-programming\\\/\",\"name\":\"PHP - Object Oriented Programming - 1 | Kindson The Genius\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/#website\"},\"datePublished\":\"2020-12-13T10:27:52+00:00\",\"dateModified\":\"2026-10-29T01:26:31+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/#\\\/schema\\\/person\\\/a2dcea4c76668e90e5842583fd5a5f60\"},\"description\":\"PHP - Object Oriented Programming - 1 \u2014 free PHP tutorial by Kindson Munonye with clear examples. Step-by-step for beginners. Part of Kindson The Genius.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/php-object-oriented-programming\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/php-object-oriented-programming\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/php-object-oriented-programming\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PHP &#8211; Object Oriented Programming &#8211; 1\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/#website\",\"url\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/\",\"name\":\"PHP Tutorial\",\"description\":\"Beginner to Expert With Practicals\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/#\\\/schema\\\/person\\\/a2dcea4c76668e90e5842583fd5a5f60\",\"name\":\"kindsonthegenius\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g\",\"caption\":\"kindsonthegenius\"},\"url\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/author\\\/kindsonthegenius-3\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PHP - Object Oriented Programming - 1 | Kindson The Genius","description":"PHP - Object Oriented Programming - 1 \u2014 free PHP tutorial by Kindson Munonye with clear examples. Step-by-step for beginners. Part of Kindson The Genius.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/kindsonthegenius.com\/php\/php-object-oriented-programming\/","og_locale":"en_US","og_type":"article","og_title":"PHP - Object Oriented Programming - 1 | Kindson The Genius","og_description":"PHP - Object Oriented Programming - 1 \u2014 free PHP tutorial by Kindson Munonye with clear examples. Step-by-step for beginners. Part of Kindson The Genius.","og_url":"https:\/\/kindsonthegenius.com\/php\/php-object-oriented-programming\/","og_site_name":"PHP Tutorial","article_published_time":"2020-12-13T10:27:52+00:00","article_modified_time":"2026-10-29T01:26:31+00:00","author":"kindsonthegenius","twitter_card":"summary_large_image","twitter_misc":{"Written by":"kindsonthegenius","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/kindsonthegenius.com\/php\/php-object-oriented-programming\/#article","isPartOf":{"@id":"https:\/\/kindsonthegenius.com\/php\/php-object-oriented-programming\/"},"author":{"name":"kindsonthegenius","@id":"https:\/\/kindsonthegenius.com\/php\/#\/schema\/person\/a2dcea4c76668e90e5842583fd5a5f60"},"headline":"PHP &#8211; Object Oriented Programming &#8211; 1","datePublished":"2020-12-13T10:27:52+00:00","dateModified":"2026-10-29T01:26:31+00:00","mainEntityOfPage":{"@id":"https:\/\/kindsonthegenius.com\/php\/php-object-oriented-programming\/"},"wordCount":691,"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/kindsonthegenius.com\/php\/php-object-oriented-programming\/","url":"https:\/\/kindsonthegenius.com\/php\/php-object-oriented-programming\/","name":"PHP - Object Oriented Programming - 1 | Kindson The Genius","isPartOf":{"@id":"https:\/\/kindsonthegenius.com\/php\/#website"},"datePublished":"2020-12-13T10:27:52+00:00","dateModified":"2026-10-29T01:26:31+00:00","author":{"@id":"https:\/\/kindsonthegenius.com\/php\/#\/schema\/person\/a2dcea4c76668e90e5842583fd5a5f60"},"description":"PHP - Object Oriented Programming - 1 \u2014 free PHP tutorial by Kindson Munonye with clear examples. Step-by-step for beginners. Part of Kindson The Genius.","breadcrumb":{"@id":"https:\/\/kindsonthegenius.com\/php\/php-object-oriented-programming\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/kindsonthegenius.com\/php\/php-object-oriented-programming\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/kindsonthegenius.com\/php\/php-object-oriented-programming\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/kindsonthegenius.com\/php\/"},{"@type":"ListItem","position":2,"name":"PHP &#8211; Object Oriented Programming &#8211; 1"}]},{"@type":"WebSite","@id":"https:\/\/kindsonthegenius.com\/php\/#website","url":"https:\/\/kindsonthegenius.com\/php\/","name":"PHP Tutorial","description":"Beginner to Expert With Practicals","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/kindsonthegenius.com\/php\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/kindsonthegenius.com\/php\/#\/schema\/person\/a2dcea4c76668e90e5842583fd5a5f60","name":"kindsonthegenius","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g","caption":"kindsonthegenius"},"url":"https:\/\/kindsonthegenius.com\/php\/author\/kindsonthegenius-3\/"}]}},"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/posts\/210","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/comments?post=210"}],"version-history":[{"count":5,"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/posts\/210\/revisions"}],"predecessor-version":[{"id":216,"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/posts\/210\/revisions\/216"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/media?parent=210"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/categories?post=210"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/tags?post=210"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}