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

PHP Class Constants

एक class constant एक fixed value है जो किसी class के अंदर रहती है और सबके द्वारा shared होती है, किसी triangle की sides की संख्या जैसी। यह कभी नहीं बदलती और आप इसे class name के through पढ़ते हैं।
Syntax
php
class ClassName {
    const CONSTANT_NAME = value;
}

echo ClassName::CONSTANT_NAME;   // outside the class
echo self::CONSTANT_NAME;        // inside the class

एक Class Constant क्या है?

एक class constant एक named value है जो सीधे किसी class body के अंदर const से define की जाती है, हर instance द्वारा shared होती है और definition के बाद बदलना possible नहीं।

Instance properties के उलट, एक constant का कोई $ sigil नहीं होता और यह किसी एक object से belong नहीं करती -- यह खुद class से belong करती है, इसलिए इसकी value class जहाँ भी इस्तेमाल हो हर जगह identical है।

उदाहरण: What Is a Class Constant?

php
<?php
// Define the class `Circle`
class Circle {
    const PI = 3.14159;
}
// Print `Circle::PI` to the output
echo Circle::PI;
?>

Class Constants Access करना

आप scope resolution operator से एक class constant पढ़ते हैं: class के बाहर से ClassName::CONST_NAME, या इसके अपने methods के अंदर से self::CONST_NAME।

क्योंकि constants किसी instance से bound नहीं होते, उन तक पहुँचने के लिए आपको कभी $this-> या एक object variable की ज़रूरत नहीं होती।

उदाहरण: Accessing Class Constants

php
<?php
// Define the class `Circle`
class Circle {
    const PI = 3.14159;
    // Define the function `area` taking `$r`
    function area($r) {
        // Return `self::PI * $r * $r` from this function
        return self::PI * $r * $r;
    }
}
// Create a new `Circle` instance, stored in `$c`
$c = new Circle();
// Print `$c->area(2) . "\n"` to the output
echo $c->area(2) . "\n";
// Print `Circle::PI` to the output
echo Circle::PI;
?>

Constants बनाम Instance Properties

एक instance property (private $rate) प्रति object अलग value रख सकती है और reassign की जा सकती है; एक class constant definition time पर fixed है और हर instance में identically shared होती है।

एक ऐसी value के लिए constant इस्तेमाल करें जो inherently class की definition का हिस्सा है -- जैसे एक fixed tax rate या एक status code -- न कि कुछ ऐसा जो प्रति object vary करता हो।

उदाहरण: Constants vs Instance Properties

php
<?php
// Define the class `Invoice`
class Invoice {
    const TAX_RATE = 0.08;
    // Declare the private property `$amount`
    private $amount;
    // Define the constructor `__construct` taking `$amount`
    function __construct($amount) {
        $this->amount = $amount;
    }
}
// Print `Invoice::TAX_RATE` to the output
echo Invoice::TAX_RATE;
?>

Constants पर Visibility

PHP 7.1 से, class constants में properties और methods जैसे ही visibility modifiers हो सकते हैं: public const, protected const, या private const।

एक private const सिर्फ खुद declaring class के अंदर से reachable है, आपको बाहर के code से internal implementation constants छुपाने देते हुए।

उदाहरण: Visibility on Constants

php
<?php
// Define the class `Config`
class Config {
    public const VERSION = "1.0";
    private const SECRET_KEY = "abc123";
    // Define the function `getKey` with no parameters
    function getKey() {
        // Return `self::SECRET_KEY` from this function
        return self::SECRET_KEY;
    }
}
// Print `Config::VERSION . "\n"` to the output
echo Config::VERSION . "\n";
// Print `(new Config())->getKey()` to the output
echo (new Config())->getKey();
?>

Constants और Enums

PHP 8.1 ने related named values के एक fixed set के लिए एक ज़्यादा structured alternative की तरह true enum types introduce किए, ऐसी type-safety के साथ जो class constants के पास नहीं होती।

Class constants एक single fixed value (जैसे एक version string या एक limit) के लिए सही tool बने रहते हैं, जबकि एक enum तब बेहतर fit करता है जब आपके पास कई named, mutually-exclusive cases हों।

उदाहरण: Constants and Enums

php
<?php
class Status {
    const ACTIVE = 'active';
    const INACTIVE = 'inactive';
}
echo Status::ACTIVE;
// PHP 8.1 enums are a more structured alternative for fixed named cases
?>
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. runtime पर एक class constant बदलने की कोशिश करना, जिसकी अनुमति नहीं है।
  2. ClassName::CONSTANT के बजाय -> से एक constant access करना।
  3. यह भूल जाना कि एक private constant class के बाहर से नहीं पढ़ा जा सकता।
चैप्टर सारांश
  • Classes और objects, constructors और destructors, और access modifiers PHP OOP का core बनाते हैं।
  • Inheritance, method overriding, abstract classes, interfaces, और traits behavior share और organize करते हैं।
  • Static members, magic methods, namespaces, Iterator interface, और class constants ज़्यादा features add करते हैं।

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.