{"id":660,"date":"2021-03-25T00:43:20","date_gmt":"2021-03-25T00:43:20","guid":{"rendered":"https:\/\/phptutorial.net\/?page_id=660"},"modified":"2025-04-07T11:24:25","modified_gmt":"2025-04-07T11:24:25","slug":"php-override-method","status":"publish","type":"page","link":"https:\/\/www.phptutorial.net\/php-oop\/php-override-method\/","title":{"rendered":"PHP Override Method"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn about the PHP overriding method and how to apply it effectively in your script.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-php-overriding-method'>Introduction to the PHP overriding method <a href=\"#introduction-to-the-php-overriding-method\" class=\"anchor\" id=\"introduction-to-the-php-overriding-method\" title=\"Anchor for Introduction to the PHP overriding method\">#<\/a><\/h2>\n\n\n\n<p>Method overriding allows a child class to provide a specific implementation of a method already provided by its parent <a href=\"https:\/\/phptutorial.net\/php-oop\/php-objects\/\">class<\/a>. <\/p>\n\n\n\n<p>To override a method, you redefine that method in the child class with the same name, parameters, and return type.<\/p>\n\n\n\n<p>The method in the parent class is called <strong>overridden method,<\/strong> while the method in the child class is known as the <strong>overriding method<\/strong>. The code in the overriding method overrides (or replaces) the code in the overridden method.<\/p>\n\n\n\n<p>PHP will decide which method (overridden or overriding method) to call based on the object used to invoke the method. <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If an object of the parent class invokes the method, PHP will execute the overridden method. <\/li>\n\n\n\n<li>But if an object of the child class invokes the method, PHP will execute the overriding method.<\/li>\n<\/ul>\n\n\n\n<p>Let&#8217;s take an example to understand method overriding better.<\/p>\n\n\n\n<p>The following example defines the <code>Robot<\/code> class that has one public method <code>greet()<\/code> and the <code>Android<\/code> class that inherits the <code>Robot<\/code> class:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Robot<\/span>\n<\/span>{\n\t<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">greet<\/span><span class=\"hljs-params\">()<\/span>\n\t<\/span>{\n\t\t<span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">'Hello!'<\/span>;\n\t}\n}\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Android<\/span> <span class=\"hljs-keyword\">extends<\/span> <span class=\"hljs-title\">Robot<\/span>\n<\/span>{\t\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>When you call the <code>greet()<\/code> method via the Android&#8217;s instance, PHP calls the <code>greet()<\/code> method of the <code>Robot<\/code> class:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$android = <span class=\"hljs-keyword\">new<\/span> Android();\n<span class=\"hljs-keyword\">echo<\/span> $android-&gt;greet(); <span class=\"hljs-comment\">\/\/ Hello!<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This is a typical <a href=\"https:\/\/phptutorial.net\/php-oop\/php-inheritance\/\">inheritance<\/a> scenario.<\/p>\n\n\n\n<p>Sometimes, you want to completely replace the method&#8217;s behavior of the parent class with a new one. In this case, you need to override the method of the parent class.<\/p>\n\n\n\n<p>To override a method, you redefine the method in the parent class again in the child class but use a different logic. <\/p>\n\n\n\n<p>The following adds the <code>greet()<\/code> method to the <code>Android<\/code> class that returns a different greeting message:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Robot<\/span>\n<\/span>{\n\t<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">greet<\/span><span class=\"hljs-params\">()<\/span>\n\t<\/span>{\n\t\t<span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">'Hello!'<\/span>;\n\t}\n}\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Android<\/span> <span class=\"hljs-keyword\">extends<\/span> <span class=\"hljs-title\">Robot<\/span>\n<\/span>{\n\t<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">greet<\/span><span class=\"hljs-params\">()<\/span>\n\t<\/span>{\n\t\t<span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">'Hi'<\/span>;\n\t}\n}\n\n$robot = <span class=\"hljs-keyword\">new<\/span> Robot();\n\n<span class=\"hljs-keyword\">echo<\/span> $robot-&gt;greet(); <span class=\"hljs-comment\">\/\/ Hello<\/span>\n\n$android = <span class=\"hljs-keyword\">new<\/span> Android();\n<span class=\"hljs-keyword\">echo<\/span> $android-&gt;greet(); <span class=\"hljs-comment\">\/\/ Hi!<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCmNsYXNzIFJvYm90CnsKCXB1YmxpYyBmdW5jdGlvbiBncmVldCgpCgl7CgkJcmV0dXJuICdIZWxsbyEnOwoJfQp9CgpjbGFzcyBBbmRyb2lkIGV4dGVuZHMgUm9ib3QKewoJcHVibGljIGZ1bmN0aW9uIGdyZWV0KCkKCXsKCQlyZXR1cm4gJ0hpJzsKCX0KfQoKJHJvYm90ID0gbmV3IFJvYm90KCk7CgplY2hvICRyb2JvdC0-Z3JlZXQoKTsgLy8gSGVsbG8KCiRhbmRyb2lkID0gbmV3IEFuZHJvaWQoKTsKZWNobyAkYW5kcm9pZC0-Z3JlZXQoKTsgLy8gSGkh\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>How it works<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, invoke the <code>greet()<\/code> method of an instance of the <code>Robot<\/code> class, the <code>greet()<\/code> method in the <code>Robot<\/code> class executes.<\/li>\n\n\n\n<li>Second, call the <code>greet()<\/code> method of an instance of the <code>Android<\/code> class, the <code>greet()<\/code> method in the <code>Android<\/code> class executes.<\/li>\n<\/ul>\n\n\n\n<p>The following class diagram illustrates the relationship between the <code>Robot<\/code> and <code>Android<\/code> classes:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" src=\"https:\/\/phptutorial.net\/wp-content\/uploads\/2021\/03\/PHP-method-overriding.svg\" alt=\"\" class=\"wp-image-666\"\/><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\" id='call-the-overridden-method-in-the-overriding-method'>Call the overridden method in the overriding method <a href=\"#call-the-overridden-method-in-the-overriding-method\" class=\"anchor\" id=\"call-the-overridden-method-in-the-overriding-method\" title=\"Anchor for Call the overridden method in the overriding method\">#<\/a><\/h2>\n\n\n\n<p>When you override a method, you will have two versions of the same method: one in the parent class and the other in the child class.<\/p>\n\n\n\n<p>If you call the method of the parent class in the method in the child class, you cannot use <code><a href=\"https:\/\/phptutorial.net\/php-oop\/php-this\/\">$this<\/a><\/code> keyword like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Android<\/span> <span class=\"hljs-keyword\">extends<\/span> <span class=\"hljs-title\">Robot<\/span>\n<\/span>{\n\t<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">greet<\/span><span class=\"hljs-params\">()<\/span>\n\t<\/span>{\n\t\t$greeting = <span class=\"hljs-keyword\">$this<\/span>-&gt;greet();\n\t\t<span class=\"hljs-keyword\">return<\/span> $greeting . <span class=\"hljs-string\">' from Android.'<\/span>;\n\t}\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>$this-&gt;greet()<\/code> will call itself indefinitely. <\/p>\n\n\n\n<p>To call the <code>greet()<\/code> method of the <code>Robot<\/code> class, you need to use the <code>parent<\/code> with the scope resolution operator <code>(::<\/code>) like the following:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Android<\/span> <span class=\"hljs-keyword\">extends<\/span> <span class=\"hljs-title\">Robot<\/span>\n<\/span>{\n\t<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">greet<\/span><span class=\"hljs-params\">()<\/span>\n\t<\/span>{\n\t\t$greeting = <span class=\"hljs-keyword\">parent<\/span>::greet();\n\t\t<span class=\"hljs-keyword\">return<\/span> $greeting . <span class=\"hljs-string\">' from Android.'<\/span>;\n\t}\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, the <code>greet()<\/code> method in the Andoird class calls the <code>greet()<\/code> method of the <code>Robot<\/code> class. It concatenates the string returned by the <code>greet()<\/code> method of the <code>Robot<\/code> method with a literal string <code>' from Android.'<\/code> and returns the concatenated string.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='more-on-php-overriding-method'>More on PHP overriding method <a href=\"#more-on-php-overriding-method\" class=\"anchor\" id=\"more-on-php-overriding-method\" title=\"Anchor for More on PHP overriding method\">#<\/a><\/h2>\n\n\n\n<p>Suppose that you need to define a new <code>CheckingAccount<\/code> class that extends the <code>BankAccount<\/code> class. The following defines the <code>BankAccount<\/code> class:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">BankAccount<\/span>\n<\/span>{\n\t<span class=\"hljs-keyword\">private<\/span> $balance;\n\n\t<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\">($amount)<\/span>\n\t<\/span>{\n\t\t<span class=\"hljs-keyword\">$this<\/span>-&gt;balance = $amount;\n\t}\n\n\t<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">getBalance<\/span><span class=\"hljs-params\">()<\/span>\n\t<\/span>{\n\t\t<span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">$this<\/span>-&gt;balance;\n\t}\n\n\t<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">deposit<\/span><span class=\"hljs-params\">($amount)<\/span>\n\t<\/span>{\n\t\t<span class=\"hljs-keyword\">if<\/span> ($amount &gt; <span class=\"hljs-number\">0<\/span>) {\n\t\t\t<span class=\"hljs-keyword\">$this<\/span>-&gt;balance += $amount;\n\t\t}\n\t\t<span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">$this<\/span>;\n\t}\n\n\t<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">withdraw<\/span><span class=\"hljs-params\">($amount)<\/span>\n\t<\/span>{\n\t\t<span class=\"hljs-keyword\">if<\/span> ($amount &gt; <span class=\"hljs-number\">0<\/span> &amp;&amp; $amount &lt;= <span class=\"hljs-keyword\">$this<\/span>-&gt;balance) {\n\t\t\t<span class=\"hljs-keyword\">$this<\/span>-&gt;balance -= $amount;\n\t\t\t<span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">true<\/span>;\n\t\t}\n\t\t<span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">false<\/span>;\n\t}\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>withdraw()<\/code> method checks if the withdrawal amount is greater than zero and less than or equal to the current balance before deducting it from the balance.<\/p>\n\n\n\n<p>Second, define the <code>CheckingAccount<\/code> class that inherits the <code>BankAccount<\/code> class. The <code>CheckingAccount<\/code> class also has the <code>withdraw()<\/code> method that overrides the <code>withdraw()<\/code> method of the <code>BankAccount<\/code> class:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">CheckingAccount<\/span> <span class=\"hljs-keyword\">extends<\/span> <span class=\"hljs-title\">BankAccount<\/span>\n<\/span>{\n\t<span class=\"hljs-keyword\">private<\/span> $minBalance;\n\n\t<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\">($amount, $minBalance)<\/span>\n\t<\/span>{\n\t\t<span class=\"hljs-keyword\">if<\/span> ($amount &gt; <span class=\"hljs-number\">0<\/span> &amp;&amp; $amount &gt;= $minBalance) {\n\t\t\t<span class=\"hljs-keyword\">parent<\/span>::__construct($amount);\n\t\t\t<span class=\"hljs-keyword\">$this<\/span>-&gt;minBalance = $minBalance;\n\t\t} <span class=\"hljs-keyword\">else<\/span> {\n\t\t\t<span class=\"hljs-keyword\">throw<\/span> <span class=\"hljs-keyword\">new<\/span> InvalidArgumentException(<span class=\"hljs-string\">'amount must be more than zero and higher than the minimum balance'<\/span>);\n\t\t}\n\t}\n\n\t<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">withdraw<\/span><span class=\"hljs-params\">($amount)<\/span>\n\t<\/span>{\n\t\t$canWithdraw = $amount &gt; <span class=\"hljs-number\">0<\/span> &amp;&amp;\n\t\t\t\t\t   <span class=\"hljs-keyword\">$this<\/span>-&gt;getBalance() - $amount &gt; <span class=\"hljs-keyword\">$this<\/span>-&gt;minBalance;\n\n\t\t<span class=\"hljs-keyword\">if<\/span> ($canWithdraw) {\n\t\t\t<span class=\"hljs-keyword\">parent<\/span>::withdraw($amount);\n\n\t\t\t<span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">true<\/span>;\n\t\t}\n\n\t\t<span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">false<\/span>;\n\t}\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>withdraw()<\/code> method in the <code>CheckingAccount<\/code> class checks the withdrawal amount against the minimum balance before deducting it.<\/p>\n\n\n\n<p>The following class diagram illustrates the relationship between the BankAccount and CheckingAccount classes:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" src=\"https:\/\/phptutorial.net\/wp-content\/uploads\/2021\/03\/PHP-overriding-method.svg\" alt=\"\" class=\"wp-image-667\"\/><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\" id='the-final-method'>The final method <a href=\"#the-final-method\" class=\"anchor\" id=\"the-final-method\" title=\"Anchor for The final method\">#<\/a><\/h2>\n\n\n\n<p>To prevent the method in the child class from overriding the method in the parent class, you can prefix the method with the <code>final<\/code> keyword:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">final<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">methodName<\/span><span class=\"hljs-params\">()<\/span> \n<\/span>{\n   <span class=\"hljs-comment\">\/\/...<\/span>\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following adds the <code>id()<\/code> method to the Robot class:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Robot<\/span>\n<\/span>{\n\t<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">greet<\/span><span class=\"hljs-params\">()<\/span>\n\t<\/span>{\n\t\t<span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">'Hello!'<\/span>;\n\t}\n\n\t<span class=\"hljs-keyword\">final<\/span> <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">id<\/span><span class=\"hljs-params\">()<\/span>\n\t<\/span>{\n\t\t<span class=\"hljs-keyword\">return<\/span> uniqid();\n\t}\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If you attempt to override the <code>id()<\/code> method from the <code>Android<\/code> class, you&#8217;ll get an error. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Android<\/span> <span class=\"hljs-keyword\">extends<\/span> <span class=\"hljs-title\">Robot<\/span>\n<\/span>{\n\t<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">greet<\/span><span class=\"hljs-params\">()<\/span>\n\t<\/span>{\n\t\t$greeting = <span class=\"hljs-keyword\">parent<\/span>::greet();\n\n\t\t<span class=\"hljs-keyword\">return<\/span> $greeting . <span class=\"hljs-string\">' from Andoid.'<\/span>;\n\t}\n\n\t<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">id<\/span><span class=\"hljs-params\">()<\/span>\n\t<\/span>{\n\t\t<span class=\"hljs-keyword\">return<\/span> uniqid(<span class=\"hljs-string\">'Android-'<\/span>);\n\t}\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Error:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">Cannot override <span class=\"hljs-keyword\">final<\/span> method Robot::id()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Method overriding allows a child class to define a method that overrides (or replaces) the method already provided by its parent class.<\/li>\n\n\n\n<li>Use <code>parent::<\/code> to call the overridden method in the overriding method.<\/li>\n\n\n\n<li>Use the final method when you don&#8217;t want a child class&#8217;s method to override a parent class&#8217;s method.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Did you find this tutorial useful?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"660\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-oop\/php-override-method\/\"\n\t\t\t\tdata-post-title=\"PHP Override Method\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"660\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-oop\/php-override-method\/\"\n\t\t\t\tdata-post-title=\"PHP Override Method\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn about the PHP overriding method and how to apply it effectively in your script.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1753,"menu_order":9,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-660","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/660","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/comments?post=660"}],"version-history":[{"count":5,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/660\/revisions"}],"predecessor-version":[{"id":3228,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/660\/revisions\/3228"}],"up":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/1753"}],"wp:attachment":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/media?parent=660"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}