{"id":129,"date":"2026-05-20T11:11:44","date_gmt":"2026-05-20T11:11:44","guid":{"rendered":"https:\/\/gigz.pk\/php\/?post_type=lesson&#038;p=129"},"modified":"2026-05-21T14:40:35","modified_gmt":"2026-05-21T14:40:35","slug":"inheritance","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/php\/?lesson=inheritance","title":{"rendered":"Inheritance"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Inheritance is one of the most important concepts in Object-Oriented Programming (OOP) in PHP. It allows one class to inherit the properties and methods of another class. This helps developers reuse code, reduce duplication, and create organized applications.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Inheritance makes it easier to manage large projects by creating relationships between parent and child classes.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Objectives<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">By the end of this training, you will be able to:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Understand the concept of inheritance in PHP<\/li>\n\n\n\n<li>Create parent and child classes<\/li>\n\n\n\n<li>Reuse methods and properties using inheritance<\/li>\n\n\n\n<li>Override methods in child classes<\/li>\n\n\n\n<li>Use the <code>parent<\/code> keyword<\/li>\n\n\n\n<li>Understand the benefits of inheritance in web development<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">What is Inheritance<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Inheritance allows a child class to access the properties and methods of a parent class.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The parent class is also called:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Base class<\/li>\n\n\n\n<li>Superclass<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The child class is also called:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Derived class<\/li>\n\n\n\n<li>Subclass<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Why Use Inheritance<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Inheritance is used to:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Reuse existing code<\/li>\n\n\n\n<li>Reduce code duplication<\/li>\n\n\n\n<li>Improve code organization<\/li>\n\n\n\n<li>Create scalable applications<\/li>\n\n\n\n<li>Simplify maintenance<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Basic Syntax of Inheritance<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>class ParentClass {<br>    \/\/ properties and methods<br>}<br><br>class ChildClass extends ParentClass {<br>    \/\/ additional properties and methods<br>}<br>?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Example of Inheritance<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>class Animal {<br><br>    public $name = \"Cat\";<br><br>    public function sound() {<br>        echo \"Animal makes a sound\";<br>    }<br>}<br><br>class Cat extends Animal {<br><br>    public function display() {<br>        echo $this-&gt;name;<br>    }<br>}<br><br>$obj = new Cat();<br><br>$obj-&gt;display();<br>$obj-&gt;sound();<br>?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Output<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>Cat<br>Animal makes a sound<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Understanding Parent Class<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A parent class contains common properties and methods that can be shared with child classes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>class Vehicle {<br><br>    public function startEngine() {<br>        echo \"Engine Started\";<br>    }<br>}<br>?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Understanding Child Class<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A child class inherits all accessible methods and properties from the parent class.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>class Car extends Vehicle {<br><br>    public function drive() {<br>        echo \"Car is moving\";<br>    }<br>}<br>?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Accessing Parent Class Methods<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>class Person {<br><br>    public function message() {<br>        echo \"Welcome\";<br>    }<br>}<br><br>class Student extends Person {<br><br>}<br><br>$obj = new Student();<br><br>$obj-&gt;message();<br>?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Method Overriding<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Method overriding happens when a child class defines a method with the same name as the parent class.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>class Bird {<br><br>    public function sound() {<br>        echo \"Bird Sound\";<br>    }<br>}<br><br>class Parrot extends Bird {<br><br>    public function sound() {<br>        echo \"Parrot Talking\";<br>    }<br>}<br><br>$obj = new Parrot();<br><br>$obj-&gt;sound();<br>?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Using the Parent Keyword<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>parent<\/code> keyword is used to access methods or constructors of the parent class.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>class Employee {<br><br>    public function company() {<br>        echo \"ABC Company\";<br>    }<br>}<br><br>class Manager extends Employee {<br><br>    public function company() {<br>        parent::company();<br>        echo \" Manager Department\";<br>    }<br>}<br><br>$obj = new Manager();<br><br>$obj-&gt;company();<br>?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Constructor Inheritance<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Child classes can inherit constructors from parent classes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>class User {<br><br>    public function __construct() {<br>        echo \"User Constructor\";<br>    }<br>}<br><br>class Admin extends User {<br><br>}<br><br>$obj = new Admin();<br>?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Types of Inheritance in PHP<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Single Inheritance<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">One child class inherits from one parent class.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Multilevel Inheritance<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A class inherits from another child class.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>class A {<br><br>    public function test1() {<br>        echo \"Class A\";<br>    }<br>}<br><br>class B extends A {<br><br>}<br><br>class C extends B {<br><br>}<br><br>$obj = new C();<br><br>$obj-&gt;test1();<br>?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Benefits of Inheritance<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Faster development<\/li>\n\n\n\n<li>Better code reusability<\/li>\n\n\n\n<li>Easy maintenance<\/li>\n\n\n\n<li>Improved readability<\/li>\n\n\n\n<li>Organized project structure<\/li>\n\n\n\n<li>Supports code scalability<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Real World Example of Inheritance<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Inheritance is commonly used in:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>School management systems<\/li>\n\n\n\n<li>E-commerce websites<\/li>\n\n\n\n<li>Banking applications<\/li>\n\n\n\n<li>Hospital management systems<\/li>\n\n\n\n<li>Employee management systems<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Example:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Parent Class: User<\/li>\n\n\n\n<li>Child Classes: Admin, Customer, Teacher, Student<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Keep parent classes general<\/li>\n\n\n\n<li>Use inheritance only when classes are related<\/li>\n\n\n\n<li>Avoid unnecessary inheritance<\/li>\n\n\n\n<li>Use meaningful class names<\/li>\n\n\n\n<li>Follow proper coding standards<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Common Errors in Inheritance<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Incorrect Class Names<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Car extends vehicle<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Class names are case-sensitive in some environments.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Accessing Private Members<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Private properties and methods cannot be accessed directly in child classes.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Inheritance is a powerful feature in PHP that promotes code reuse and better application structure. It helps developers build scalable and maintainable web applications using Object-Oriented Programming principles.<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/php\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">Advanced PHP > Object-Oriented PHP > Inheritance<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1779275454604\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n","protected":false},"menu_order":39,"template":"","class_list":["post-129","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Inheritance - Learn PHP with GiGz.PK<\/title>\n<meta name=\"description\" content=\"Learn inheritance in PHP with examples, parent and child classes, method overriding, and OOP concepts for web development.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/php\/?lesson=inheritance\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Inheritance - Learn PHP with GiGz.PK\" \/>\n<meta property=\"og:description\" content=\"Learn inheritance in PHP with examples, parent and child classes, method overriding, and OOP concepts for web development.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/php\/?lesson=inheritance\" \/>\n<meta property=\"og:site_name\" content=\"Learn PHP with GiGz.PK\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-21T14:40:35+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/php\\\/?lesson=inheritance\",\"url\":\"https:\\\/\\\/gigz.pk\\\/php\\\/?lesson=inheritance\",\"name\":\"Inheritance - Learn PHP with GiGz.PK\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/php\\\/#website\"},\"datePublished\":\"2026-05-20T11:11:44+00:00\",\"dateModified\":\"2026-05-21T14:40:35+00:00\",\"description\":\"Learn inheritance in PHP with examples, parent and child classes, method overriding, and OOP concepts for web development.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/php\\\/?lesson=inheritance#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/php\\\/?lesson=inheritance\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/php\\\/?lesson=inheritance#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/php\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Advanced PHP > Object-Oriented PHP > Inheritance\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/php\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/php\\\/\",\"name\":\"Learn PHP with GiGz.PK\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/php\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Inheritance - Learn PHP with GiGz.PK","description":"Learn inheritance in PHP with examples, parent and child classes, method overriding, and OOP concepts for web development.","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:\/\/gigz.pk\/php\/?lesson=inheritance","og_locale":"en_US","og_type":"article","og_title":"Inheritance - Learn PHP with GiGz.PK","og_description":"Learn inheritance in PHP with examples, parent and child classes, method overriding, and OOP concepts for web development.","og_url":"https:\/\/gigz.pk\/php\/?lesson=inheritance","og_site_name":"Learn PHP with GiGz.PK","article_modified_time":"2026-05-21T14:40:35+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/php\/?lesson=inheritance","url":"https:\/\/gigz.pk\/php\/?lesson=inheritance","name":"Inheritance - Learn PHP with GiGz.PK","isPartOf":{"@id":"https:\/\/gigz.pk\/php\/#website"},"datePublished":"2026-05-20T11:11:44+00:00","dateModified":"2026-05-21T14:40:35+00:00","description":"Learn inheritance in PHP with examples, parent and child classes, method overriding, and OOP concepts for web development.","breadcrumb":{"@id":"https:\/\/gigz.pk\/php\/?lesson=inheritance#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/php\/?lesson=inheritance"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/php\/?lesson=inheritance#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/php"},{"@type":"ListItem","position":2,"name":"Advanced PHP > Object-Oriented PHP > Inheritance"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/php\/#website","url":"https:\/\/gigz.pk\/php\/","name":"Learn PHP with GiGz.PK","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/php\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/php\/index.php?rest_route=\/wp\/v2\/lesson\/129","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/php\/index.php?rest_route=\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/php\/index.php?rest_route=\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/php\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=129"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}