{"id":7324,"date":"2019-10-21T06:17:34","date_gmt":"2019-10-21T06:17:34","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=7324"},"modified":"2021-11-16T05:44:32","modified_gmt":"2021-11-16T05:44:32","slug":"complete-reference-guide-of-php-classes-objects","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/complete-reference-guide-of-php-classes-objects\/","title":{"rendered":"Complete reference guide of PHP Classes &amp; Objects !"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">What is Object Oriented Programming<\/h2>\n\n\n\n<p>Object-Oriented Programming (OOP) is a programming method that is based on the concept of classes and objects. As known to procedural programming where the focus is on writing procedures or functions that perform operations on the data, in object-oriented programming the focus is on the creations of objects which contain both data and functions together.<\/p>\n\n\n\n<p>Object-oriented programming has many advantages over conventional or procedural style of programming. The most important ones are listed below:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>It provides a clear modular structure for the programs.<\/li><li>&#8220;don&#8217;t repeat yourself&#8221; (DRY) principle, and thus make your code much easier to maintain, modify and debug.<\/li><li>It makes it possible to create more complicated behavior with less code and shorter development time and high degree of re usability.<\/li><\/ul>\n\n\n\n<p>The following sections will describe how classes and objects work in PHP.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p> <strong>Tip:<\/strong>&nbsp;The idea behind Don&#8217;t Repeat Yourself (DRY) principle is reducing the repetition of code by abstracting out the code that are common for the application and placing them at a single place and reuse them instead of repeating it. <\/p><\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">Understanding Classes and Objects<\/h2>\n\n\n\n<p>Classes and objects are the two main parts of object-oriented programming. A class is a self-contained, independent collection of variables and functions which work together to perform one or more specific tasks, while objects are individual instances of a class.<\/p>\n\n\n\n<p>A class acts as a template or blueprint from which lots of individual objects can be created. When individual objects are created, they inherit the same generic properties and behaviors, although each object may have different values for certain properties.<\/p>\n\n\n\n<p>For example, think of a class as a blueprint for a house. The blueprint itself is not a house, but is a a plan of the house. While, an object is like an actual house built according to that blueprint. We can build several identical houses from the same blueprint, but each house may have different paints, interiors and families inside, as shown in the illustration below.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"658\" height=\"240\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2019\/10\/class-object-relationship-illustration.png\" alt=\"\" class=\"wp-image-7326\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2019\/10\/class-object-relationship-illustration.png 658w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2019\/10\/class-object-relationship-illustration-300x109.png 300w\" sizes=\"auto, (max-width: 658px) 100vw, 658px\" \/><\/figure>\n\n\n\n<p>A class can be declared using the&nbsp;<code>class<\/code>&nbsp;keyword, followed by the name of the class and a pair of curly braces (<code>{}<\/code>), as shown in the following example.<\/p>\n\n\n\n<p>Let&#8217;s create a PHP file named Home.php and put the following example code inside it so that our class code should be separated from rest of the program. We can then use it wherever it&#8217;s needed by simply including the Home.php file.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Home<\/span>\n<\/span>{\n    <span class=\"hljs-comment\">\/\/ Declare  properties<\/span>\n    <span class=\"hljs-keyword\">public<\/span> $length = <span class=\"hljs-number\">0<\/span>;\n    <span class=\"hljs-keyword\">public<\/span> $width = <span class=\"hljs-number\">0<\/span>;\n    \n    <span class=\"hljs-comment\">\/\/ Method to get the perimeter<\/span>\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">getPerimeter<\/span><span class=\"hljs-params\">()<\/span><\/span>{\n        <span class=\"hljs-keyword\">return<\/span> (<span class=\"hljs-number\">2<\/span> * (<span class=\"hljs-keyword\">$this<\/span>-&gt;length + <span class=\"hljs-keyword\">$this<\/span>-&gt;width));\n    }\n    \n    <span class=\"hljs-comment\">\/\/ Method to get the area<\/span>\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">getArea<\/span><span class=\"hljs-params\">()<\/span><\/span>{\n        <span class=\"hljs-keyword\">return<\/span> (<span class=\"hljs-keyword\">$this<\/span>-&gt;length * <span class=\"hljs-keyword\">$this<\/span>-&gt;width);\n    }\n}\n<span class=\"hljs-meta\">?&gt;<\/span><\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p> The&nbsp;<code>public<\/code>&nbsp;keyword before the properties and methods in the example above, is an&nbsp;<a href=\"https:\/\/www.tutorialrepublic.com\/php-tutorial\/php-classes-and-objects.php#oop-access-modifier\" target=\"_blank\" rel=\"noopener\">access modifier<\/a>, which indicates that this property or method is accessible from anywhere. We will learn more about this a little later in this chapter. <\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p> <strong>Note:<\/strong>&nbsp;Syntactically, variables within a class are called&nbsp;<em>properties<\/em>, whereas functions are called&nbsp;<em>methods<\/em>. Also class names conventionally are written in PascalCase i.e. each concatenated word starts with an uppercase letter (e.g. MyClass). <\/p><\/blockquote>\n\n\n\n<p>Once a class has been defined, objects can be created from the class with the&nbsp;<code>new<\/code>&nbsp;keyword. Class methods and properties can directly be accessed through this object instance.<\/p>\n\n\n\n<p>Create another PHP file name test.php and put the following code inside it.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n<span class=\"hljs-comment\">\/\/ Include class definition<\/span>\n<span class=\"hljs-keyword\">require<\/span> <span class=\"hljs-string\">\"Home.php\"<\/span>;\n \n<span class=\"hljs-comment\">\/\/ Create a new object from Homeclass<\/span>\n$obj = <span class=\"hljs-keyword\">new<\/span> Home;\n \n<span class=\"hljs-comment\">\/\/ Get the object properties values<\/span>\n<span class=\"hljs-keyword\">echo<\/span> $obj-&gt;length; <span class=\"hljs-comment\">\/\/ 0utput: 0<\/span>\n<span class=\"hljs-keyword\">echo<\/span> $obj-&gt;width; <span class=\"hljs-comment\">\/\/ 0utput: 0<\/span>\n \n<span class=\"hljs-comment\">\/\/ Set object properties values<\/span>\n$obj-&gt;length = <span class=\"hljs-number\">30<\/span>;\n$obj-&gt;width = <span class=\"hljs-number\">20<\/span>;\n \n<span class=\"hljs-comment\">\/\/ Read the object properties values again to show the change<\/span>\n<span class=\"hljs-keyword\">echo<\/span> $obj-&gt;length; <span class=\"hljs-comment\">\/\/ 0utput: 30<\/span>\n<span class=\"hljs-keyword\">echo<\/span> $obj-&gt;width; <span class=\"hljs-comment\">\/\/ 0utput: 20<\/span>\n \n \n<span class=\"hljs-comment\">\/\/ Call the object methods<\/span>\n<span class=\"hljs-keyword\">echo<\/span> $obj-&gt;getPerimeter(); <span class=\"hljs-comment\">\/\/ 0utput: 100<\/span>\n<span class=\"hljs-keyword\">echo<\/span> $obj-&gt;getArea(); <span class=\"hljs-comment\">\/\/ Output: 600<\/span>\n<span class=\"hljs-meta\">?&gt;<\/span><\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The arrow symbol (<code>-&gt;<\/code>) is an OOP construct that is used to access contained properties and methods of a given object. Whereas, the pseudo-variable&nbsp;<code>$this<\/code>&nbsp;provides a reference to the calling object i.e. the object to which the method belongs.<\/p>\n\n\n\n<p>The real power of object oriented programming becomes evident when using multiple instances of the same class, as shown in the following example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n<span class=\"hljs-comment\">\/\/ Include class definition<\/span>\n<span class=\"hljs-keyword\">require<\/span> <span class=\"hljs-string\">\"Home.php\"<\/span>;\n \n<span class=\"hljs-comment\">\/\/ Create multiple objects from the Home class<\/span>\n$obj1 = <span class=\"hljs-keyword\">new<\/span> Rectangle;\n$obj2 = <span class=\"hljs-keyword\">new<\/span> Rectangle;\n \n<span class=\"hljs-comment\">\/\/ Call the methods of both the objects<\/span>\n<span class=\"hljs-keyword\">echo<\/span> $obj1-&gt;getArea(); <span class=\"hljs-comment\">\/\/ Output: 0<\/span>\n<span class=\"hljs-keyword\">echo<\/span> $obj2-&gt;getArea(); <span class=\"hljs-comment\">\/\/ Output: 0<\/span>\n \n<span class=\"hljs-comment\">\/\/ Set $obj1 properties values<\/span>\n$obj1-&gt;length = <span class=\"hljs-number\">30<\/span>;\n$obj1-&gt;width = <span class=\"hljs-number\">20<\/span>;\n \n<span class=\"hljs-comment\">\/\/ Set $obj2 properties values<\/span>\n$obj2-&gt;length = <span class=\"hljs-number\">35<\/span>;\n$obj2-&gt;width = <span class=\"hljs-number\">50<\/span>;\n \n<span class=\"hljs-comment\">\/\/ Call the methods of both the objects again<\/span>\n<span class=\"hljs-keyword\">echo<\/span> $obj1-&gt;getArea(); <span class=\"hljs-comment\">\/\/ Output: 600<\/span>\n<span class=\"hljs-keyword\">echo<\/span> $obj2-&gt;getArea(); <span class=\"hljs-comment\">\/\/ Output: 1750<\/span>\n<span class=\"hljs-meta\">?&gt;<\/span><\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>As you can see in the above example, calling the&nbsp;<code>getArea()<\/code>&nbsp;method on different objects causes that method to operate on a different set of data. Each object instance is completely independent, with its own properties and methods, and thus can be manipulated independently, even if they&#8217;re of the same class.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using Constructors and Destructors<\/h2>\n\n\n\n<p>To make the object-oriented programming easier, PHP provides some magic methods that are executed automatically when certain actions occur within an object.<\/p>\n\n\n\n<p>For example, the magic method&nbsp;<code>__construct()<\/code>&nbsp;(known as&nbsp;<em>constructor<\/em>) is executed automatically whenever a new object is created. Similarly, the magic method&nbsp;<code>__destruct()<\/code>&nbsp;(known as&nbsp;<em>destructor<\/em>) is executed automatically when the object is destroyed. A destructor function cleans up any resources allocated to an object once the object is destroyed.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">MyClass<\/span>\n<\/span>{\n    <span class=\"hljs-comment\">\/\/ Constructor<\/span>\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">__construct<\/span><span class=\"hljs-params\">()<\/span><\/span>{\n        <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">'The class \"'<\/span> . <span class=\"hljs-keyword\">__CLASS__<\/span> . <span class=\"hljs-string\">'\" was initiated!&lt;br&gt;'<\/span>;\n    }\n    \n    <span class=\"hljs-comment\">\/\/ Destructor<\/span>\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">__destruct<\/span><span class=\"hljs-params\">()<\/span><\/span>{\n        <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">'The class \"'<\/span> . <span class=\"hljs-keyword\">__CLASS__<\/span> . <span class=\"hljs-string\">'\" was destroyed.&lt;br&gt;'<\/span>;\n    }\n}\n \n<span class=\"hljs-comment\">\/\/ Create a new object<\/span>\n$obj = <span class=\"hljs-keyword\">new<\/span> MyClass;\n \n<span class=\"hljs-comment\">\/\/ Output a message at the end of the file<\/span>\n<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"The end of the file is reached.\"<\/span>;\n<span class=\"hljs-meta\">?&gt;<\/span><\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The PHP code in the above example will produce the following output:The class &#8220;MyClass&#8221; was initiated!<br>The end of the file is reached.<br>The class &#8220;MyClass&#8221; was destroyed.<\/p>\n\n\n\n<p>A destructor is called automatically when a scripts ends. However, to explicitly trigger the destructor, you can destroy the object using the PHP&nbsp;<code>unset()<\/code>&nbsp;function, as follow:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">MyClass<\/span>\n<\/span>{\n    <span class=\"hljs-comment\">\/\/ Constructor<\/span>\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">__construct<\/span><span class=\"hljs-params\">()<\/span><\/span>{\n        <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">'The class \"'<\/span> . <span class=\"hljs-keyword\">__CLASS__<\/span> . <span class=\"hljs-string\">'\" was initiated!&lt;br&gt;'<\/span>;\n    }\n    \n    <span class=\"hljs-comment\">\/\/ Destructor<\/span>\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">__destruct<\/span><span class=\"hljs-params\">()<\/span><\/span>{\n    <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">'The class \"'<\/span> . <span class=\"hljs-keyword\">__CLASS__<\/span> . <span class=\"hljs-string\">'\" was destroyed.&lt;br&gt;'<\/span>;\n    }\n}\n \n<span class=\"hljs-comment\">\/\/ Create a new object<\/span>\n$obj = <span class=\"hljs-keyword\">new<\/span> MyClass;\n \n<span class=\"hljs-comment\">\/\/ Destroy the object<\/span>\n<span class=\"hljs-keyword\">unset<\/span>($obj);\n \n<span class=\"hljs-comment\">\/\/ Output a message at the end of the file<\/span>\n<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"The end of the file is reached.\"<\/span>;\n<span class=\"hljs-meta\">?&gt;<\/span><\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>\n\nNow, the PHP code in the above example will produce the following output:\n\n<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The class &#8220;MyClass&#8221; was initiated!<\/li><li>The class &#8220;MyClass&#8221; was destroyed.<\/li><li>The end of the file is reached. <\/li><\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p> <strong>Tip:<\/strong>&nbsp;PHP automatically clean up all resources allocated during execution when the script is finished, e.g. closing database connections, destroying objects, etc. <\/p><\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p> <strong>Note:<\/strong>&nbsp;The&nbsp;<code>__CLASS__<\/code>&nbsp;is a&nbsp;<a href=\"https:\/\/www.tutorialrepublic.com\/php-tutorial\/php-magic-constants.php\" target=\"_blank\" rel=\"noopener\">magic constant<\/a>&nbsp;which contains the name of the class in which it is occur. It is empty, if it occurs outside of the class.<br><br> <\/p><\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">Extending Classes through Inheritance<\/h2>\n\n\n\n<p>Classes can inherit the properties and methods of another class using the&nbsp;<code>extends<\/code>&nbsp;keyword. This process of extensibility is called inheritance. It is probably the most powerful reason behind using the object-oriented programming model.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n<span class=\"hljs-comment\">\/\/ Include class definition<\/span>\n<span class=\"hljs-keyword\">require<\/span> <span class=\"hljs-string\">\"Rectangle.php\"<\/span>;\n \n<span class=\"hljs-comment\">\/\/ Define a new class based on an existing class<\/span>\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Square<\/span> <span class=\"hljs-keyword\">extends<\/span> <span class=\"hljs-title\">Rectangle<\/span>\n<\/span>{   \n    <span class=\"hljs-comment\">\/\/ Method to test if the rectangle is also a square<\/span>\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">isSquare<\/span><span class=\"hljs-params\">()<\/span><\/span>{\n        <span class=\"hljs-keyword\">if<\/span>(<span class=\"hljs-keyword\">$this<\/span>-&gt;length == <span class=\"hljs-keyword\">$this<\/span>-&gt;width){\n            <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">true<\/span>; <span class=\"hljs-comment\">\/\/ Square<\/span>\n        } <span class=\"hljs-keyword\">else<\/span>{\n            <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">false<\/span>; <span class=\"hljs-comment\">\/\/ Not a square<\/span>\n        }\n    }\n}\n \n<span class=\"hljs-comment\">\/\/ Create a new object from Square class<\/span>\n$obj = <span class=\"hljs-keyword\">new<\/span> Square;\n \n<span class=\"hljs-comment\">\/\/ Set object properties values<\/span>\n$obj-&gt;length = <span class=\"hljs-number\">20<\/span>;\n$obj-&gt;width = <span class=\"hljs-number\">20<\/span>;\n \n<span class=\"hljs-comment\">\/\/ Call the object methods<\/span>\n<span class=\"hljs-keyword\">if<\/span>($obj-&gt;isSquare()){\n    <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"The area of the square is \"<\/span>;\n} <span class=\"hljs-keyword\">else<\/span>{\n    <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"The area of the rectangle is \"<\/span>;\n};\n<span class=\"hljs-keyword\">echo<\/span> $obj-&gt;getArea();\n<span class=\"hljs-meta\">?&gt;<\/span><\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The PHP code in the above example will produce the following output:<br>&#8212;&#8211;The area of the square is 400<\/p>\n\n\n\n<p>As you can see in the above example, even though the class definition of Square doesn&#8217;t explicitly contain&nbsp;<code>getArea()<\/code>&nbsp;method nor the&nbsp;<code>$length<\/code>&nbsp;and&nbsp;<code>$width<\/code>&nbsp;property, instances of the Square class can use them, as they inherited from the parent Rectangle class.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p> <strong>Tip:<\/strong>&nbsp;Since a child class is derived from a parent class, it is also referred to as a derived class, and its parent is called the base class. <\/p><\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"oop-access-modifier\">Controlling the Visibility of Properties and Methods<\/h2>\n\n\n\n<p>When working with classes, you can even restrict access to its properties and methods using the&nbsp;<em>visibility keywords<\/em>&nbsp;for greater control. There are three visibility keywords (from most visible to least visible):&nbsp;<code>public<\/code>,&nbsp;<code>protected<\/code>,&nbsp;<code>private<\/code>, which determines how and from where properties and methods can be accessed and modified.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>public<\/strong>&nbsp;\u2014 A public property or method can be accessed anywhere, from within the class and outside. This is the default visibility for all class members in PHP.<\/li><li><strong>protected<\/strong>&nbsp;\u2014 A protected property or method can only be accessed from within the class itself or in child or inherited classes i.e. classes that extends that class.<\/li><li><strong>private<\/strong>&nbsp;\u2014 A private property or method is accessible only from within the class that defines it. Even child or inherited classes cannot access private properties or methods.<\/li><\/ul>\n\n\n\n<p>The following example will show you how this visibility actually works:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n<span class=\"hljs-comment\">\/\/ Class definition<\/span>\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Automobile<\/span>\n<\/span>{\n    <span class=\"hljs-comment\">\/\/ Declare  properties<\/span>\n    <span class=\"hljs-keyword\">public<\/span> $fuel;\n    <span class=\"hljs-keyword\">protected<\/span> $engine;\n    <span class=\"hljs-keyword\">private<\/span> $transmission;\n}\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Car<\/span> <span class=\"hljs-keyword\">extends<\/span> <span class=\"hljs-title\">Automobile<\/span>\n<\/span>{\n    <span class=\"hljs-comment\">\/\/ Constructor<\/span>\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">__construct<\/span><span class=\"hljs-params\">()<\/span><\/span>{\n        <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">'The class \"'<\/span> . <span class=\"hljs-keyword\">__CLASS__<\/span> . <span class=\"hljs-string\">'\" was initiated!&lt;br&gt;'<\/span>;\n    }\n}\n \n<span class=\"hljs-comment\">\/\/ Create an object from Automobile class<\/span>\n$automobile = <span class=\"hljs-keyword\">new<\/span> Automobile;\n \n<span class=\"hljs-comment\">\/\/ Attempt to set $automobile object properties<\/span>\n$automobile-&gt;fuel = <span class=\"hljs-string\">'Petrol'<\/span>; <span class=\"hljs-comment\">\/\/ ok<\/span>\n$automobile-&gt;engine = <span class=\"hljs-string\">'1500 cc'<\/span>; <span class=\"hljs-comment\">\/\/ fatal error<\/span>\n$automobile-&gt;transmission = <span class=\"hljs-string\">'Manual'<\/span>; <span class=\"hljs-comment\">\/\/ fatal error<\/span>\n \n<span class=\"hljs-comment\">\/\/ Create an object from Car class<\/span>\n$car = <span class=\"hljs-keyword\">new<\/span> Car;\n \n<span class=\"hljs-comment\">\/\/ Attempt to set $car object properties<\/span>\n$car-&gt;fuel = <span class=\"hljs-string\">'Diesel'<\/span>; <span class=\"hljs-comment\">\/\/ ok<\/span>\n$car-&gt;engine = <span class=\"hljs-string\">'2200 cc'<\/span>; <span class=\"hljs-comment\">\/\/ fatal error<\/span>\n$car-&gt;transmission = <span class=\"hljs-string\">'Automatic'<\/span>; <span class=\"hljs-comment\">\/\/ undefined<\/span>\n<span class=\"hljs-meta\">?&gt;<\/span><\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Static Properties and Methods<\/h2>\n\n\n\n<p>In addition to the visibility, properties and methods can also be declared as&nbsp;<code>static<\/code>, which makes them accessible without needing an instantiation of the class. Static properties and methods can be accessed using the scope resolution operator (<code>::<\/code>), like this:&nbsp;<code>ClassName::$property<\/code>&nbsp;and&nbsp;<code>ClassName::method()<\/code>.<\/p>\n\n\n\n<p>A property declared as static cannot be accessed via the object of that class though a static method can be, as demonstrated in the following example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n<span class=\"hljs-comment\">\/\/ Class definition<\/span>\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">HelloClass<\/span>\n<\/span>{\n    <span class=\"hljs-comment\">\/\/ Declare a static property<\/span>\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">static<\/span> $greeting = <span class=\"hljs-string\">\"Hello World!\"<\/span>;\n    \n    <span class=\"hljs-comment\">\/\/ Declare a static method<\/span>\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">sayHello<\/span><span class=\"hljs-params\">()<\/span><\/span>{\n        <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-keyword\">self<\/span>::$greeting;\n    }\n}\n<span class=\"hljs-comment\">\/\/ Attempt to access static property and method directly<\/span>\n<span class=\"hljs-keyword\">echo<\/span> HelloClass::$greeting; <span class=\"hljs-comment\">\/\/ Output: Hello World!<\/span>\nHelloClass::sayHello(); <span class=\"hljs-comment\">\/\/ Output: Hello World!<\/span>\n \n<span class=\"hljs-comment\">\/\/ Attempt to access static property and method via object<\/span>\n$hello = <span class=\"hljs-keyword\">new<\/span> HelloClass;\n<span class=\"hljs-keyword\">echo<\/span> $hello-&gt;greeting; <span class=\"hljs-comment\">\/\/ Strict Warning<\/span>\n$hello-&gt;sayHello(); <span class=\"hljs-comment\">\/\/ Output: Hello World!<\/span>\n<span class=\"hljs-meta\">?&gt;<\/span><\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The keyword&nbsp;<code>self<\/code>&nbsp;in the above example means &#8220;the current class&#8221;. It is never preceded by a dollar sign (<code>$<\/code>) and always followed by the&nbsp;<code>::<\/code>&nbsp;operator (e.g.&nbsp;<code>self::$name<\/code>).<\/p>\n\n\n\n<p>The&nbsp;<code>self<\/code>&nbsp;keyword is different from the&nbsp;<code>this<\/code>&nbsp;keyword which means &#8220;the current object&#8221; or&nbsp; &#8220;the current instance of a class&#8221;. The&nbsp;<code>this<\/code>&nbsp;keyword is always preceded by a dollar sign (<code>$<\/code>) and followed by the&nbsp;<code>-&gt;<\/code>&nbsp;operator (e.g.&nbsp;<code>$this-&gt;name<\/code>).<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p> <strong>Note:<\/strong>&nbsp;Since static methods can be called without an instance of a class (i.e. object), the pseudo-variable&nbsp;<code>$this<\/code>&nbsp;is not available inside the method declared as static. <\/p><\/blockquote>\n\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_45850\"><figure class=\"wp-block-embed wp-block-embed-youtube is-type-video is-provider-youtube epyt-figure\"><div class=\"wp-block-embed__wrapper\"><iframe loading=\"lazy\"  id=\"_ytid_89594\"  width=\"760\" height=\"427\"  data-origwidth=\"760\" data-origheight=\"427\" src=\"https:\/\/www.youtube.com\/embed\/?enablejsapi=1&#038;autoplay=0&#038;cc_load_policy=0&#038;cc_lang_pref=&#038;iv_load_policy=1&#038;loop=0&#038;rel=1&#038;fs=1&#038;playsinline=0&#038;autohide=2&#038;theme=dark&#038;color=red&#038;controls=1&#038;disablekb=0&#038;\" class=\"__youtube_prefs__  no-lazyload\" title=\"YouTube player\"  data-epytgalleryid=\"epyt_gallery_45850\"  allow=\"fullscreen; accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen data-no-lazy=\"1\" data-skipgform_ajax_framebjll=\"\"><\/iframe><\/div><\/figure><div class=\"epyt-gallery-list\"><div>Sorry, there was a YouTube error.<\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>What is Object Oriented Programming Object-Oriented Programming (OOP) is a programming method that is based on the concept of classes and objects. As known to procedural programming where the focus is on writing procedures or functions that perform operations on the data, in object-oriented programming the focus is on the creations of objects which contain&#8230;<\/p>\n","protected":false},"author":14,"featured_media":7343,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","_joinchat":[],"footnotes":""},"categories":[5150],"tags":[5434,5437,5436,177],"class_list":["post-7324","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php","tag-class","tag-classobject","tag-object","tag-php"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/7324","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=7324"}],"version-history":[{"count":4,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/7324\/revisions"}],"predecessor-version":[{"id":25370,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/7324\/revisions\/25370"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media\/7343"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=7324"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=7324"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=7324"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}