PHP Abstract Class In Hindi

📔 : PHP 🔗

बाकी Object oriented Languages की तरह ही PHP में भी Abstraction का concept है। आप PHP में Abstract Class define कर सकते हैं।


PHP में abstract class को incomplete class भी कहते हैं , ऐसी class में हम सिर्फ अपने logics का architecture तैयार करते हैं उसे implement किसी और class में करते हैं। abstract class का आप Object नहीं बना सकते हैं , और अगर class में एक भी method as a abstract defined हुआ तो class abstract ही होगी।


abstract class / methods को abstract keyword की help से ही define करते हैं , और अगर कोई भी method as a abstract define हो गया तो उसे हम उस class में implement नहीं करते हैं। because abstract class का मतलब ही है कि methods / variables का architecture तैयार करना न कि उसे implement करना।

हालाँकि ऐसा नहीं की abstract class में method implement नहीं कर सकते हैं , आप implement कर सकते हैं but आप उस method को access उसकी child class से ही कर पायंगे because abstract class का Object नहीं बनता।


abstract class में defined methods को implement करने के लिए हम Inheritance का use करके इसके child class में implement करते हैं।

PHP Abstract Class Example

File : php_abstract_class.php

Copy Fullscreen Close Fullscreen
<?php 
	abstract class AbstractClass
	{
	    /*define abstract method*/	    
	    abstract protected function calculate($number1, $number2);
	}

	class ChildClass extends AbstractClass
	{
		/*implment abstract method*/
	    protected function calculate($number1 , $number2) {
	        return $number1 * $number2;
	    }

	    /*public method so that we can call it from outside of class*/
	   	public function product($number1 , $number2)
	    {
	    	echo "Product is : ". $this->calculate($number1 , $number2);
	    }
	}

	$obj = new ChildClass();
	$obj->product(12, 34);
?>
Output
Product is : 408

Some Important Points-
  1. आप Abstract class का Object नहीं बना सकते। ऐसा करने पर Fatal Error generate होगी।
    Fatal error: Uncaught Error: Cannot instantiate abstract class

  2. Abstract class में defined सभी Abstract methods का इसके child class में implement करना mandatory है , implement न करने पर Fatal Error generate होगी।
    Fatal error: Class ChildClass contains n abstract method and must therefore be declared abstract or implement the remaining methods

  3. Abstract class में defined Abstract method में आपको required parameters define करने की जरूरत होती है , optional parameters को Abstract method में आप skip कर सकते हैं।

  4. चूंकि PHP multiple Inheritance को support नहीं करती है इसलिए , आप एक से ज्यादा Abstract Class को extend नहीं कर सकते हैं।

  5. आप Abstract class में defined method की visibility को child class में update कर सकते हैं।

Understanding Visibility

Visibility का मतलब Property / Method का scope define करना होता है। की वह Property / Method कहाँ - कहाँ Accessible होगा। PHP में Class Visibility 3 Types की होती है। जिन्हे Access Modifiers भी कहते हैं।
Read More About It...


Calling Parent Class Constructor & Destructor

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


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

File : php_abstract_class.php

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

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

		public abstract function dog();
	}

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

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

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

	$obj = new MyClass();
	$obj->dog();

?>
Output
Animal's class constructor
MyClass's class constructor
Dogs bark 
Animal's class destructor
MyClass's class destructor

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