PHP में Object-Oriented Programming (OOP) की मदद से हम अपने कोड को ज़्यादा organized, reusable और modular बना सकते हैं। OOP की कई महत्वपूर्ण विशेषताएं होती हैं, जैसे कि Class, Object, Inheritance, Polymorphism और Encapsulation। इन सबके साथ एक और महत्वपूर्ण कांसेप्ट है – Static Members.
इस लेख में हम विस्तार से समझेंगे कि PHP OOP में Static Members क्या होते हैं, क्यों ज़रूरी होते हैं, और इन्हें कैसे प्रयोग किया जाता है।
Static Members क्या हैं?
Static members PHP के Object-Oriented Programming (OOP) का एक important concept है जो class level पर काम करता है। ये object-specific नहीं होते और इन्हें बिना object बनाए directly access किया जा सकता है।
- Static Members का मतलब होता है – ऐसी properties (variables) और methods (functions) जो किसी class के instance (object) पर नहीं बल्कि class खुद पर लागू होते हैं।
- सामान्यतः जब हम किसी class की कोई property या method बनाते हैं, तो हमें उसका object बनाना पड़ता है और फिर उस object के माध्यम से हम उस property या method को access करते हैं।
- लेकिन Static Members को हम class के नाम से ही access कर सकते हैं — बिना object बनाए।
Types of Static Members:
- Static Properties – Class का shared variable (सभी objects के लिए common)
- Static Methods – Class का standalone function (बिना object के चलता है)
Static Properties:
यह एक class-level variable होता है जो सभी objects में same रहता है। एक ऐसा variable होता है जो class के अंदर declare किया जाता है और उसे static keyword के साथ लिखा जाता है। इसे class के सभी objects के बीच share किया जाता है, यानी static property की एक ही copy रहती है।
class MyClass {
public static $count = 0; // Static property declare
} PHPclass Visitor {
public static $visitorCount = 0;
public function __construct() {
self::$visitorCount++; // Har naye visitor pe ++
}
}
// बिना object बनाए access
echo Visitor::$visitorCount; // Output: 0
// Objects create karo
$visitor1 = new Visitor();
$visitor2 = new Visitor();
echo Visitor::$visitorCount; // Output: 2 (Shared across all objects) PHPKey Point:
self::का use करते हैं static property access करने के लिए।$visitorCountसभी objects के लिए same है।
Static Method:
Static method भी class के अंदर define किया जाता है और इसे भी static keyword के साथ लिखा जाता है। Static method को भी object नहीं बल्कि class नाम से call किया जाता है।
class MathHelper {
public static function add($a, $b) {
return $a + $b;
}
}
// Call without object
echo MathHelper::add(5, 3); // Output: 8 PHPImportant Rule:
- Static method के अंदर non-static members को access नहीं कर सकते (क्योंकि वो object पर depend करते हैं)।
- Only other static members ही access कर सकते हैं।
Static और self Keyword?
जब हम class के अंदर किसी static property या method को access करना चाहते हैं, तो हम self:: keyword का उपयोग करते हैं।
class MathHelper {
public static function square($num) {
return $num * $num;
}
public static function cube($num) {
return $num * self::square($num);
}
}
echo MathHelper::cube(3); // Output: 27
PHPयहाँ cube() method के अंदर हमने square() को self:: के साथ call किया।
Why Use Static Members?
- Memory Efficient – Single copy सभी objects के लिए share होती है।
- Utility Functions – जैसे
MathHelper::add(),Logger::log()जैसे common tasks के लिए बेस्ट। - No Object Needed – बिना object बनाए quick operations कर सकते हैं।
Limitations Of Static Members?
$thiskeyword use नहीं कर सकते (क्योंकि object होता ही नहीं)।- Non-static properties/methods को static method में access नहीं कर सकते।
Late Static Binding
PHP में एक खास सुविधा है जिसे Late Static Binding कहते हैं। जब हम किसी static method को subclass से call करते हैं, तो static:: keyword का प्रयोग real class को target करता है।
class A {
public static function who() {
echo __CLASS__;
}
public static function test() {
static::who(); // Late static binding
}
}
class B extends A {
public static function who() {
echo __CLASS__;
}
}
B::test(); // Output: B
PHPयहाँ पर static binding की वजह से सही class (B) की method call होती है।
Conclusion
PHP में Static Members एक powerful feature हैं, जो developers को efficient और reusable code लिखने में मदद करते हैं। जब हमें किसी value या function की ज़रूरत होती है जो object-specific न होकर class-specific हो, तब हम static members का उपयोग करते हैं।
मुख्य बिंदु:
- Static Members को class से directly access किया जाता है।
- Static properties की एक ही copy रहती है।
- Static methods object की बजाय logic या utility के लिए होते हैं।
self::औरstatic::keywords का सही उपयोग करना ज़रूरी है।


Welcome to HindiTutorials, your go-to platform for learning and mastering concepts in Hindi! Our mission is to make education accessible and simple for everyone by providing high-quality tutorials on programming, technology, and various other topics.