{"id":769,"date":"2018-11-14T14:22:18","date_gmt":"2018-11-14T14:22:18","guid":{"rendered":"https:\/\/barkhane.com\/php\/2018\/11\/14\/inheritance-php\/"},"modified":"2024-03-03T14:58:12","modified_gmt":"2024-03-03T14:58:12","slug":"inheritance-php","status":"publish","type":"post","link":"https:\/\/barkhane.com\/php\/inheritance-php\/","title":{"rendered":"Inheritance in php"},"content":{"rendered":"<div id=\"pl-769\" class=\"panel-layout\">\n<div id=\"pg-769-0\" class=\"panel-grid panel-no-style\" data-ratio=\"1\" data-ratio-direction=\"right\">\n<div id=\"pgc-769-0-0\" class=\"panel-grid-cell\" data-weight=\"1\">\n<div id=\"panel-769-0-0-0\" class=\"so-panel widget widget_sow-editor panel-first-child panel-last-child\" data-index=\"0\" data-style=\"{&quot;background_image_attachment&quot;:false,&quot;background_display&quot;:&quot;tile&quot;}\">\n<div class=\"so-widget-sow-editor so-widget-sow-editor-base\">\n<h2>Inheritance in PHP<\/h2>\n<p>Inheritance is the php oops concept which is based around the concept of base classes or superclasses and derived <a href=\"https:\/\/barkhane.com\/php\/classes-in-php\/\">classes<\/a> or subclasses.<\/p>\n<p>Base classes or Super Classes are also known as parent classes and similarly, derived classes or subclasses are known as child classes.<\/p>\n<p>In the real world as a child take features from their parents, Same way in oops, child class inherit properties of their parent class with the help of inheritance.<\/p>\n<p>We use `extends` keyword to implement inheritance in php.<\/p>\n<p>Advantage of using inheritance in php oops, we can share properties of one class to other class.<\/p>\n<\/div>\n<p>In simple words Inheritance\u00a0is When a class is defined by inheriting existing function of a parent class then it is called inheritance. Here child class will inherit all or few member functions and variables of a parent class.<\/p>\n<p><b>Parent class<\/b>\u00a0\u2212 A class that is inherited from by another class. This is also called a base class or super class.<\/p>\n<p><b>Child Class<\/b>\u00a0\u2212 A class that inherits from another class. This is also called a subclass or derived class.<\/p>\n<\/div>\n<\/div>\n<p class=\"ui_qtext_para\"><strong>Types of inheritance in PHP:<\/strong><\/p>\n<ul>\n<li>Inheritance has three type, single, multiple and multi level inheritance.<\/li>\n<li>It supports only single inheritance, where only one class can be derived from single parent class.<\/li>\n<li>We can simulate multiple inheritance by using interfaces.<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<p>For Example :<\/p>\n<div id=\"pl-769\" class=\"panel-layout\">\n<div id=\"pg-769-0\" class=\"panel-grid panel-no-style\" data-ratio=\"1\" data-ratio-direction=\"right\">\n<div id=\"pgc-769-0-0\" class=\"panel-grid-cell\" data-weight=\"1\">\n<div id=\"panel-769-0-0-0\" class=\"so-panel widget widget_sow-editor panel-first-child panel-last-child\" data-index=\"0\" data-style=\"{&quot;background_image_attachment&quot;:false,&quot;background_display&quot;:&quot;tile&quot;}\">\n<div class=\"so-widget-sow-editor so-widget-sow-editor-base\">\n<div class=\"one-third\">\n<div class=\"table\">\n<div class=\"table-row\">\n<div class=\"table-cell\">\n<pre><code>\n&lt;?php\n\nclass\u00a0Foo\n{\npublic\u00a0function\u00a0printItem($string)\n{\necho\u00a0\\'Foo:\u00a0\\'\u00a0.\u00a0$string\u00a0.\u00a0PHP_EOL;\n}\n\npublic\u00a0function\u00a0printPHP()\n{\necho\u00a0\\'PHP\u00a0is\u00a0great.\\'\u00a0.\u00a0PHP_EOL;\n}\n}\n\nclass\u00a0Bar\u00a0extends\u00a0Foo\n{\npublic\u00a0function\u00a0printItem($string)\n{\necho\u00a0\\'Bar:\u00a0\\'\u00a0.\u00a0$string\u00a0.\u00a0PHP_EOL;\n}\n}\n\n$foo\u00a0=\u00a0new\u00a0Foo();\n$bar\u00a0=\u00a0new\u00a0Bar();\n$foo-&gt;printItem(\\'baz\\');\u00a0\/\/\u00a0Output:\u00a0\\'Foo:\u00a0baz\\'\n$foo-&gt;printPHP();\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/\u00a0Output:\u00a0\\'PHP\u00a0is\u00a0great\\'\n$bar-&gt;printItem(\\'baz\\');\u00a0\/\/\u00a0Output:\u00a0\\'Bar:\u00a0baz\\'\n$bar-&gt;printPHP();\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/\u00a0Output:\u00a0\\'PHP\u00a0is\u00a0great\\'\n\n?&gt;\n<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Inheritance in PHP Inheritance is the php oops concept which is based around the concept of base classes or superclasses [&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-769","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/posts\/769","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=769"}],"version-history":[{"count":0,"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/posts\/769\/revisions"}],"wp:attachment":[{"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/media?parent=769"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/categories?post=769"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/tags?post=769"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}