PHP Class Constants
In this page:
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
// Define the class `Circle`
class Circle {
const PI = 3.14159;
}
// Print `Circle::PI` to the output
echo Circle::PI;
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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;
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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;
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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();
?>
Login to try C/C++/Java/PHP code in the editor
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
class Status {
const ACTIVE = 'active';
const INACTIVE = 'inactive';
}
echo Status::ACTIVE;
// PHP 8.1 enums are a more structured alternative for fixed named cases
?>
Login to try C/C++/Java/PHP code in the editor
- runtime पर एक class constant बदलने की कोशिश करना, जिसकी अनुमति नहीं है।
ClassName::CONSTANTके बजाय->से एक constant access करना।- यह भूल जाना कि एक
privateconstant 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 करते हैं।
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: