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 करके बताइए।