← Back to PHP Course | Chapter 8: OOP | Lesson 10 of 14

PHP Static Methods और Properties

Static का मतलब है कि कुछ पूरी class से belong करता है न कि एक object से, school bell जैसा जो सबके लिए बजती है। आप पहले एक object बनाए बिना इसे इस्तेमाल कर सकते हैं।
Syntax
php
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
<?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);
?>

Static Methods

आप class name और :: (scope resolution operator) इस्तेमाल करके एक static method call करते हैं, जैसे MathHelper::square(4), न कि -> से किसी object के through।

उदाहरण: Static Methods

php
<?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);
?>

Static Properties

Static properties calls के बीच अपनी value retain करती हैं और class के सभी instances द्वारा shared होती हैं, जो उन्हें एक counter जैसी चीज़ों के लिए उपयोगी बनाता है जो track करता है कि किसी class के अब तक कितने objects बनाए गए।

उदाहरण: Static Properties

php
<?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;
?>

Static parent Calls

Static methods $this access नहीं कर सकते, क्योंकि कोई specific object instance नहीं है जिस पर वे चल रहे हों — वे सिर्फ static properties के साथ या explicitly pass किए गए parameters के साथ काम कर सकते हैं।

उदाहरण: Static parent Calls

php
<?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");
?>

Static कब इस्तेमाल करें

Static members उन utility functions के लिए एक अच्छा fit हैं जो किसी particular object की state पर depend नहीं करते, जैसे एक formatting helper, लेकिन प्रति-object अलग होनी चाहिए ऐसी चीज़ों के लिए static state को overuse करना code को test करना मुश्किल बना सकता है।

उदाहरण: When to Use Static

php
<?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");
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. किसी static method के अंदर $this इस्तेमाल करना, जहाँ कोई object नहीं है।
  2. :: के बजाय -> से एक static method call करना।
  3. यह भूल जाना कि एक static property class के हर instance द्वारा shared होती है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.