← Back to PHP Course | Chapter 8: OOP | Lesson 7 of 14

PHP Abstract Classes

एक abstract class एक आधे-अधूरे blueprint जैसी है जिसे आप सीधे नहीं बना सकते। यह कुछ parts set करती है और बाकी child classes के पूरे करने के लिए छोड़ देती है।
Syntax
php
abstract class ClassName {
    abstract function method_name();   // no body
}

class ChildClass extends ClassName {
    function method_name() {
        // implementation required
    }
}

एक Abstract Class क्या है?

abstract keyword से declare की गई एक abstract class को new से सीधे instantiate नहीं किया जा सकता — यह सिर्फ extend होने के लिए exist करती है, अपनी subclasses के लिए shared structure define करने वाले एक partial template की तरह serve करते हुए।

उदाहरण: What is an Abstract Class?

php
<?php
// Define the class `Shape`
abstract class Shape {
    // Define the function `area` with no parameters
    abstract function area();
}
// Define the class `Square`, inheriting from `Shape`
class Square extends Shape {
    // Define the function `area` with no parameters
    function area() {
        // Return `16` from this function
        return 16;
    }
}
// Create a new `Square` instance, stored in `$sq`
$sq = new Square();
// Print `$sq->area()` to the output
echo $sq->area();
?>

Abstract Methods Define करना

किसी abstract class के अंदर abstract methods की कोई body नहीं होती — सिर्फ एक signature — हर concrete (non-abstract) subclass को अपनी implementation देने पर मजबूर करते हुए, नहीं तो PHP एक fatal error raise करेगा।

उदाहरण: Defining Abstract Methods

php
<?php
// Define the class `Shape`
abstract class Shape {
    // Define the function `area` with no parameters
    abstract function area();
}
// Define the class `Circle`, inheriting from `Shape`
class Circle extends Shape {
    // Define the function `area` with no parameters
    function area() {
        // Return `3.14 * 5 * 5` from this function
        return 3.14 * 5 * 5;
    }
}
// Create a new `Circle` instance, stored in `$c`
$c = new Circle();
// Print `$c->area()` to the output
echo $c->area();
?>

Abstract Methods Implement करना

Abstract classes अपनी abstract methods के साथ-साथ regular, पूरी तरह-implemented methods और properties भी रख सकती हैं, आपको common logic share करने देते हुए साथ ही subclasses से genuinely अलग parts भरवाते हुए।

उदाहरण: Implementing Abstract Methods

php
<?php
// Define the class `Shape`
abstract class Shape {
    // Define the function `describe` with no parameters
    function describe() {
        // Print "This is a shape with area: " to the output
        echo "This is a shape with area: ";
    }
    // Define the function `area` with no parameters
    abstract function area();
}
// Define the class `Square`, inheriting from `Shape`
class Square extends Shape {
    // Define the function `area` with no parameters
    function area() {
        // Return `9` from this function
        return 9;
    }
}
// Create a new `Square` instance, stored in `$sq`
$sq = new Square();
$sq->describe();
// Print `$sq->area()` to the output
echo $sq->area();
?>

Regular Methods वाली Abstract Classes

एक abstract class तब इस्तेमाल करें जब कई related classes को common state/behavior और कुछ specific methods के लिए एक mandatory contract दोनों share करना हो, एक plain interface के उलट, जो shared implementation नहीं रख सकता।

उदाहरण: Abstract Classes with Regular Methods

php
<?php
// Define the class `Employee`
abstract class Employee {
    // Define the function `clockIn` with no parameters
    function clockIn() {
        // Print "Clocked in\n" to the output
        echo "Clocked in\n";
    }
    // Define the function `calculatePay` with no parameters
    abstract function calculatePay();
}
// Define the class `Hourly`, inheriting from `Employee`
class Hourly extends Employee {
    // Define the function `calculatePay` with no parameters
    function calculatePay() {
        // Return `15 * 40` from this function
        return 15 * 40;
    }
}
// Create a new `Hourly` instance, stored in `$emp`
$emp = new Hourly();
$emp->clockIn();
// Print `$emp->calculatePay()` to the output
echo $emp->calculatePay();
?>

Parameter Matching Rules

एक typical example एक abstract PaymentMethod class है जिसमें एक shared logTransaction() method plus एक abstract processPayment() method है, जहाँ CreditCard और PayPal subclasses हर एक अपने तरीके से processPayment() implement करते हैं।

उदाहरण: Parameter Matching Rules

php
<?php
// Define the class `PaymentMethod`
abstract class PaymentMethod {
    // Define the function `logTransaction` with no parameters
    function logTransaction() {
        // Print "Transaction logged\n" to the output
        echo "Transaction logged\n";
    }
    // Define the function `processPayment` with no parameters
    abstract function processPayment();
}
// Define the class `CreditCard`, inheriting from `PaymentMethod`
class CreditCard extends PaymentMethod {
    // Define the function `processPayment` with no parameters
    function processPayment() {
        // Print "Processing credit card payment" to the output
        echo "Processing credit card payment";
    }
}
// Create a new `CreditCard` instance, stored in `$payment`
$payment = new CreditCard();
$payment->logTransaction();
$payment->processPayment();
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. new से किसी abstract class का object बनाने की कोशिश करना, जो एक fatal error है।
  2. child class में हर abstract method implement न करना, जो child को भी abstract बना देता है।
  3. किसी abstract method को एक body देना, जिसकी अनुमति नहीं है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.