PHP Abstract Classes

PHP

PHP Abstract Classes

एक abstract क्लास को किसी भी दुसरे PHP Class की तरह ही घोषित किया जाता है, सिर्फ उस क्लास के आगे abstract कीवर्ड को लगाया जाता है।

एक abstract क्लास एक ऐसा क्लास है जिसमें कम से कम एक abstract मेथड होती है। और यह abstract मेथड declared तो किया जाता है, लेकिन कोड में लागू नहीं किया जाता है।

एक abstract क्लास या मेथड को abstract कीवर्ड द्वारा परिभाषित किया जाता है।

एक abstract क्लास से इनहेरिट करते समय, child class method में निम्नलिखित नियम होने चाहिए:

  • एक child क्लास मेथड को उसी नाम से परिभाषित किया जाना चाहिए, जिस नाम से parent abstract मेथड है।
  • एक child क्लास मेथड को एक समान या कम access modifier के साथ परिभाषित किया जाना चाहिए।
  • Required arguments की संख्या एक समान होनी चाहिए। हालाँकि, एक चाइल्ड क्लास के खुद की अतिरिक्त optional arguments हो सकते हैं।
उदाहरण: PHP Abstract Class
<?php
// Parent class
abstract class Books{
    public $title;
    public function __construct($title){
        $this->title = $title;
    }
    abstract public function titleName();
}

// Child class
class Maths extends Books{
    public function titleName(){
        return "Maths book name is: $this->title!";
    }
}

// Creating objects from the child class
$maths = new Maths("Encyclopedias of Mathematics");
echo $maths->titleName();

?>

PHP Abstract Class with arguments

आइए एक और उदाहरण देखें जहां abstract method में एक argument है

उदाहरण: PHP Abstract Class with arguments
<?php
// Parent class
abstract class Calculation{
    // Abstract method with arguments
    abstract public function addition($a, $b);
}

// Child class
class ChildClass extends Calculation{
    public function addition($a, $b){
        return $a + $b;
    }
}

// Creating objects from the child class
$obj = new ChildClass();
echo $obj->addition(10, 20);

?>

PHP Abstract Class with optional argument in child class

आइए एक और उदाहरण देखें जहां abstract method में argument है, और child class में एक optional arguments हैं जो parent's abstract method में परिभाषित नहीं हैं।

उदाहरण: with optional argument in child class
<?php
abstract class Books{
    // Abstract method एक argument के साथ
    abstract protected function titleName($title);
}

class Maths extends Books{
    // child class में optional argument है $author जो parent's abstract method में नहीं हैं
    public function titleName($title, $author = "."){
        return "The author of $title is $author";
    }
}

$math = new Maths;
echo $math->titleName("Encyclopedias of Mathematics", "James Stuart Tanton");
?>

एक abstract क्लास एक ऐसा क्लास है जिसमें कम से कम एक abstract मेथड होती है। और यह abstract मेथड declared तो किया जाता है, लेकिन कोड में लागू नहीं किया जाता है। इसका क्या मतलब है इसे हम नीचे एक उदाहरण से समझते हैं

उदाहरण: With body in abstract method
<?php
abstract class ParentClass{
    abstract function printdata(){
        echo "Parent class printdata method";
    }
}
?>
Output
Fatal error: Abstract function ParentClass::printdata()
cannot contain body

ऊपर दिए गए उदाहरण में हमने abstract method बनाया printdata नाम का,और उसमें प्रिंट कराने के लिए कुछ कोड डाल दिया।

जैसा कि आप ऊपर देख सकते हैं हमने echo कराने की कोशिश की "Parent class printdata method".

और अब जैसे ही हम इसे एग्जीक्यूट करने की कोशिश करेंगे यह में एक error मैसेज Fatal error: Abstract function ParentClass::printdata() cannot contain body डिस्प्ले करता है

ऐसा इसलिए हुआ क्योंकि हम पहले भी देख चुके हैं की abstract मेथड डिक्लेअर को किया जा सकता है लेकिन उसके अंदर कोई भी कोड नहीं डाल सकते है।

Article By: Brajlal Prasad
Created on: 13 Jun 2023  1114  Views
 Print Article
Report Error

If you want to report an error, or any suggestion please send us an email to [email protected]