PHP Static Methods और Properties
In this page:
class ClassName {
public static $property = value;
static function method_name() { }
}
ClassName::method_name();
ClassName::$property;
Static Members क्या हैं?
static keyword से declare किया गया एक static method या property किसी individual object के बजाय खुद class से belong करता है, यानी यह हर instance में shared होता है और बिना किसी instance के भी call किया जा सकता है।
उदाहरण: What are Static Members?
<?php
// Define the class `MathHelper`
class MathHelper {
// Define the function `square` taking `$n`
static function square($n) {
// Return `$n * $n` from this function
return $n * $n;
}
}
// Print `MathHelper::square(4)` to the output
echo MathHelper::square(4);
?>
Login to try C/C++/Java/PHP code in the editor
Static Methods
आप class name और :: (scope resolution operator) इस्तेमाल करके एक static method call करते हैं, जैसे MathHelper::square(4), न कि -> से किसी object के through।
उदाहरण: Static Methods
<?php
// Define the class `MathHelper`
class MathHelper {
// Define the function `square` taking `$n`
static function square($n) {
// Return `$n * $n` from this function
return $n * $n;
}
}
// Print `MathHelper::square(5)` to the output
echo MathHelper::square(5);
?>
Login to try C/C++/Java/PHP code in the editor
Static Properties
Static properties calls के बीच अपनी value retain करती हैं और class के सभी instances द्वारा shared होती हैं, जो उन्हें एक counter जैसी चीज़ों के लिए उपयोगी बनाता है जो track करता है कि किसी class के अब तक कितने objects बनाए गए।
उदाहरण: Static Properties
<?php
// Define the class `Counter`
class Counter {
static $count = 0;
// Define the constructor `__construct` with no parameters
function __construct() {
self::$count++;
}
}
new Counter();
new Counter();
// Print `Counter::$count` to the output
echo Counter::$count;
?>
Login to try C/C++/Java/PHP code in the editor
Static parent Calls
Static methods $this access नहीं कर सकते, क्योंकि कोई specific object instance नहीं है जिस पर वे चल रहे हों — वे सिर्फ static properties के साथ या explicitly pass किए गए parameters के साथ काम कर सकते हैं।
उदाहरण: Static parent Calls
<?php
// Define the class `Formatter`
class Formatter {
static $prefix = "LOG: ";
// Define the function `format` taking `$msg`
static function format($msg) {
// Return `self::$prefix . $msg` from this function
return self::$prefix . $msg;
}
}
// Print `Formatter::format("Started")` to the output
echo Formatter::format("Started");
?>
Login to try C/C++/Java/PHP code in the editor
Static कब इस्तेमाल करें
Static members उन utility functions के लिए एक अच्छा fit हैं जो किसी particular object की state पर depend नहीं करते, जैसे एक formatting helper, लेकिन प्रति-object अलग होनी चाहिए ऐसी चीज़ों के लिए static state को overuse करना code को test करना मुश्किल बना सकता है।
उदाहरण: When to Use Static
<?php
// Define the class `StringHelper`
class StringHelper {
// Define the function `slugify` taking `$text`
static function slugify($text) {
// Return `strtolower(str_replace(" ", "-", $text))` from this function
return strtolower(str_replace(" ", "-", $text));
}
}
// Print `StringHelper::slugify("Hello World")` to the output
echo StringHelper::slugify("Hello World");
?>
Login to try C/C++/Java/PHP code in the editor
- किसी static method के अंदर
$thisइस्तेमाल करना, जहाँ कोई object नहीं है। ::के बजाय->से एक static method call करना।- यह भूल जाना कि एक static property class के हर instance द्वारा shared होती है।
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: