PHP Abstract Classes
In this page:
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
// 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();
?>
Login to try C/C++/Java/PHP code in the editor
Abstract Methods Define करना
किसी abstract class के अंदर abstract methods की कोई body नहीं होती — सिर्फ एक signature — हर concrete (non-abstract) subclass को अपनी implementation देने पर मजबूर करते हुए, नहीं तो PHP एक fatal error raise करेगा।
उदाहरण: Defining Abstract Methods
<?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();
?>
Login to try C/C++/Java/PHP code in the editor
Abstract Methods Implement करना
Abstract classes अपनी abstract methods के साथ-साथ regular, पूरी तरह-implemented methods और properties भी रख सकती हैं, आपको common logic share करने देते हुए साथ ही subclasses से genuinely अलग parts भरवाते हुए।
उदाहरण: Implementing Abstract Methods
<?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();
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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();
?>
Login to try C/C++/Java/PHP code in the editor
Parameter Matching Rules
एक typical example एक abstract PaymentMethod class है जिसमें एक shared logTransaction() method plus एक abstract processPayment() method है, जहाँ CreditCard और PayPal subclasses हर एक अपने तरीके से processPayment() implement करते हैं।
उदाहरण: Parameter Matching Rules
<?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();
?>
Login to try C/C++/Java/PHP code in the editor
newसे किसी abstract class का object बनाने की कोशिश करना, जो एक fatal error है।- child class में हर abstract method implement न करना, जो child को भी abstract बना देता है।
- किसी abstract method को एक body देना, जिसकी अनुमति नहीं है।
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: