Static properties वो properties होती हैं, जो class के instance (object) से independent (स्वतंत्र) होती हैं। इसका मतलब यह है कि static properties को class के object बनाए बिना भी access किया जा सकता है।
Static properties को class के भीतर define किया जाता है और इनके लिए static कीवर्ड का इस्तेमाल किया जाता है।
Static Properties का Declaration
Static property को declare करने के लिए static कीवर्ड का इस्तेमाल किया जाता है। उदाहरण के लिए:-
class MyClass {
public static $count = 0;
}
यहां पर count एक static property है, जिसे MyClass class में define किया गया है।
Static Properties का Access करना
Static properties को access करने के लिए, हम :: (double colon) operator का उपयोग करते हैं। Static properties को object के माध्यम से access नहीं किया जा सकता, बल्कि class के नाम से access किया जाता है।
उदाहरण के लिए:-
class MyClass {
public static $count = 0;
public static function incrementCount() {
self::$count++;
}
}
// Static property को access करना
echo MyClass::$count; // Output: 0
// Static method को call करना
MyClass::incrementCount();
// Static property को फिर से access करना
echo MyClass::$count; // Output: 1
इस उदाहरण में, हमने MyClass class में एक static property $count और एक static method incrementCount() बनाई है। incrementCount() method static property को increment करता है। फिर, हमने MyClass::$count से static property को access किया और उसकी value को output किया।
इसे पढ़ें:–
निवेदन:- इस पोस्ट को अपने दोस्तों के साथ share कीजिए और अपने सवाल नीचे comment करके बताइए।
Summary (सारांश)
Static Properties in PHP वे class के वे properties होते हैं जिन्हें बिना object बनाए ही access किया जा सकता है। यह article पूरी तरह से समझाता है कि static keyword का use करके static properties को कैसे declare किया जाता है, और इन्हें :: (double colon) operator के माध्यम से class name से कैसे access किया जाता है। इसमें उदाहरणों के साथ incrementCount() जैसे static methods का भी प्रयोग दिखाया गया है। इस article से छात्र सीखेंगे:
- Static properties क्या होते हैं और इनका declaration कैसे करें।
- Static properties को object के बिना class name से access करने का तरीका।
- Static methods के साथ static properties को कैसे modify किया जाता है।
- self:: and static:: keywords के उपयोग की समझ।