{"id":814,"date":"2018-11-15T03:47:53","date_gmt":"2018-11-15T03:47:53","guid":{"rendered":"https:\/\/barkhane.com\/php\/2018\/11\/15\/overriding-in-php\/"},"modified":"2024-03-03T14:53:23","modified_gmt":"2024-03-03T14:53:23","slug":"overriding-in-php","status":"publish","type":"post","link":"https:\/\/barkhane.com\/php\/overriding-in-php\/","title":{"rendered":"Overriding in PHP"},"content":{"rendered":"<p id=\"e3ec\" class=\"graf graf--p graf-after--h3\">Overriding in PHP: In the context of object oriented programming method overriding means to replace parent method in child class.In overriding we can re-declare parent class method in child class. So, basically the purpose of overriding is to change the behavior of your parent <a href=\"https:\/\/barkhane.com\/php\/classes-in-php\/\">class<\/a> method.<\/p>\n<p id=\"19ec\" class=\"graf graf--p graf-after--p\">When your class has some method and another class(derived class) want the same method with different behavior then using overriding you can completely change the behavior of base class(or parent class). The two methods with the same name and same\u00a0<strong class=\"markup--strong markup--p-strong\">parameter is called overriding.<\/strong><\/p>\n<p id=\"fa52\" class=\"graf graf--p graf-after--p\">Overriding context is not so difficult in PHP. As we all know overriding in PHP is a method of updating the inherited method from parent class to child class. So, in this case you will require method with same name in parent class as well as in child class and then we will see how the behavior of parent class method is changed when child class override the method of parent class.<\/p>\n<p id=\"8946\" class=\"graf graf--p graf-after--p\">In OOP PHP if we were to create a method in the child class having the same name, same number of parameters and the same access specifier as in it\u2019s parent then we can say that we are doing method overriding. for example:<\/p>\n<pre>class ParentClass {\n    public function test($param) {\n        return \\\"\\\\n Parent - the parameter value is $param\\\";\n    }\n}\nclass ChildClass extends ParentClass {\n    public function test($param) {\n        echo \\\"\\\\n Child - the parameter value is $param\\\";\n    }\n}\n$objParentClass = new ParentClass;\n$objChildClass = new ChildClass;\n$objParentClass-&gt;test(\\'class ParentClass\\');\n$objChildClass-&gt;test(\\'class ChildClass\\');<\/pre>\n<p id=\"cfb6\" class=\"graf graf--p graf-after--pre graf--trailing\">The definition of <a href=\"https:\/\/barkhane.com\/php\/polymorphism-in-php\/\">polymorphism<\/a> says that \u201cif the decision to invoke a method is made by inspecting the object at run time then it is a case of polymorphism\u201d. We can apply this rule in our example. Whether to call method test() of class ParentClass or class ChildClass is decided by inspecting the concerned object which is making the call to the method. Thus the method call will be based on the object.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Overriding in PHP: In the context of object oriented programming method overriding means to replace parent method in child class.In [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[3],"tags":[],"class_list":["post-814","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/posts\/814","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/comments?post=814"}],"version-history":[{"count":0,"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/posts\/814\/revisions"}],"wp:attachment":[{"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/media?parent=814"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/categories?post=814"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/tags?post=814"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}