PHP Inheritance In Hindi

📔 : PHP 🔗

किसी class को या Class की properties / behavior को extend करना Inheritance कहते हैं। जिस class को extend किया गया है उसे Parent Class , जिस class के द्वारा extend किया गया है उसे Child Class कहते हैं। Extend करने पर Child Class में लगभग बो सभी properties / methods होंगे जो Parent Class में हैं, हालाँकि यह Parent Class के access modifiers पर depend करता है कि बो Properties Accessible हैं या नहीं ।


PHP में किसी class को inherit करने के लिए extend keyword का use किया जाता है।


PHP मुख्य रूप से 3 Types की Inheritance को support करती है -

  1. Single Inheritance
  2. Multilevel Inheritance
  3. Hierarchical Inheritance

PHP Single Inheritance

Inheritance का यह सबसे easy way है , जिसमे किसी single class को किसी single class के द्वारा Inherit किया जाता है।
Example :

File : php_single_inheritance.php

CopyFullscreenClose Fullscreen
<?php
        class A
	{
		function methodA()
		{
			echo 'Class A methodA';
		}
	}

	class B extends A
	{
		function methodB()
		{
			echo 'Class B methodB';
		}
	}

	$obj = new B();
	$obj->methodA();
	echo '<br>'; /*For line break*/
	$obj->methodB();
?>
Output
Class A methodA
Class B methodB

? Inheritance में Child Class के द्वारा Parent Class की सिर्फ public & protected Properties / Methods को access किया जा सकता है , private नहीं। By default Class की  सभी Properties / Methods public होते हैं।


PHP Multilevel Inheritance

multilevel inheritance में किसी Class को दूसरी class के द्वारा inherit किया जाता है और फिर उस class को किसी तीसरी class के द्वारा inherit किया जाता है।
Exxample :

File : php_Multilevel_inheritance.php

CopyFullscreenClose Fullscreen
<?php
        class A
	{
		function methodA()
		{
			echo 'Class A methodA';
		}
	}

	class B extends A
	{
		function methodB()
		{
			echo 'Class B methodB';
		}
	}

	class C extends B
	{
		function methodC()
		{
			echo 'Class C methodC';
		}
	}

	$obj = new C();
	$obj->methodA();
	echo '<br>'; 
	$obj->methodB();
	echo '<br>'; 
	$obj->methodC();
?>
Output
Class A methodA
Class B methodB
Class C methodC

PHP Hierarchical Inheritance

इस type के Inheritance में किसी single class को एक से ज्यादा Classes के द्वारा Inherit किया जाता है।
Example :

File : php_hierarchical_inheritance.php

CopyFullscreenClose Fullscreen
<?php
        class A
	{
		function methodA()
		{
			echo 'Class A methodA';
		}
	}

	class B extends A
	{
		function methodB()
		{
			echo 'Class B methodB';
		}
	}

	class C extends A
	{
		function methodC()
		{
			echo 'Class C methodC';
		}
	}

	$obj = new C();
	$obj->methodA();
	echo '<br>';
        $obj->methodC();
	/*now initialize one more Object for class B*/
	echo '<br>';
	$newObj = new B();
	$newObj->methodA();
?>
Output
Class A methodA
Class A methodA
Class C methodC

ऊपर दिए गए Example में Class A को दो classes B,C द्वारा inherit किया गया है।

PHP parent

जिस तरह से class के अंदर उस class की Properties / Methods को access करने के लिए $this-> & self use किये जाते हैं, उसी तरह से Inheritance में Parent Class की Properties / Methods को access करने के लिए parent keyword का use किया जाता है।

Important
हालाँकि यह जरूरी नहीं है कि Parent Class की Properties / Methods को access करने के लिए parent keyword का ही use करें , $this-> & self का use भी कर सकते हैं , क्योंकि जब class को Inherit किया जाता है तो सभी public , protected Properties / Methods child class में bind हो जाते हैं। लेकिन parent का use करने से code complexity कम हो जाती है, जिससे समझने में आसानी होती है।


Calling Parent Class Constructor & Destructor

Parent Class में आप constructor & destructor define कर सकते हैं , जब child class का Object बनता है तो ये automatically run होते हैं। हालाँकि अगर Parent Class को extend करने वाली child class में भी constructor & destructor define हैं तो Paret Class में defined constructor & destructor run नहीं होंगे।


parent class के constructor & destructor run करने के लिए parent::__construct() और parent::__destruct() का use किया जाता है।

File : php_call_construct_destruct.php

CopyFullscreenClose Fullscreen
<?php 
	class Animal
	{
		function __construct()
		{
			echo "Animal's class constructor";
		}

		function __destruct()
		{
			echo "Animal's class destructor";
		}	
	}

	class Dog extends Animal
	{
		function __construct()
		{
			/*calling parent class constructor*/
			parent::__construct();
			echo "Dog's class constructor";
		}

		function __destruct()
		{
			/*calling parent class destructor*/
			parent::__destruct();
			echo "Dog's class destructor";
		}

		public function bark()
		{
			echo 'Dogs bark';
		}
	}

	$obj = new Dog();
	$obj->bark();
?>
Output
Animal's class constructor
Dog's class constructor
Dogs bark Animal's class destructor
Dog's class destructor

हालाँकि parent::__construct() और parent::__destruct() को आप अपनी need के according आगे - पीछे भी call कर सकते हैं।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers