- Introduction to Magic Constants in PHP in Hindi

Introduction to Magic Constants in PHP
Magic constants PHP में पूर्वनिर्धारित constants हैं जो उनके उपयोग के आधार पर बदल जाते हैं। वे डबल अंडरस्कोर (__) से शुरू होते हैं और डबल अंडरस्कोर के साथ समाप्त होते हैं।
वे अन्य पूर्वनिर्धारित constants के समान हैं लेकिन जैसा कि वे संदर्भ के साथ अपने मूल्यों को बदलते हैं, उन्हें magic constants कहा जाता है।
PHP में नौ magic constants जिसमें आठ magic constants डबल अंडरस्कोर (__) से शुरू और खत्म होते हैं।
- __LINE__
- __FILE__
- __DIR__
- __FUNCTION__
- __CLASS__
- __TRAIT__
- __METHOD__
- __NAMESPACE__
- ClassName::class
सभी constants, regular constant के विपरीत, run-time के बजाय compile-time पर हल किए जाते हैं। magic constants केस-असंवेदनशील हैं।
Changelog
संस्करण | विवरण |
5.3.0 | जोड़ा गया __DIR__ और __NAMESPACE__ magic constant |
5.4.0 | जोड़ा गया __TRAIT__ magic |
5.5.0 | जोड़ा गया ::class magic constant |
सभी constants नीचे दिए गए उदाहरण कोड के साथ परिभाषित किए गए हैं:
1. __LINE__
यह फ़ाइल की वर्तमान पंक्ति संख्या लौटाता है, जहाँ इस constant का उपयोग किया जाता है।
उदाहरण:
<?php
echo "<h3>Example for __LINE__</h3>";
// print Your current line number i.e;4
echo "You are at line number " . __LINE__ . "<br><br>";
?>
आउटपुट:

2. __FILE__:
यह magic constant निष्पादित फ़ाइल का पूरा Path देता है, जहाँ फ़ाइल संग्रहीत होती है। यदि इसे include के अंदर उपयोग किया जाता है, तो included फ़ाइल का नाम वापस आता है।
उदाहरण:
<?php
echo "<h3>Example for __FILE__</h3>";
//print full path of file with .php extension
echo __FILE__ . "<br><br>";
?>
आउटपुट:

3. __ DIR__:
यह निष्पादित फ़ाइल का full directory path देता है। इस magic constant द्वारा लौटाया गया रास्ता dirname(__ FILE__) के बराबर है। जब तक यह रूट डाइरेक्टरी नहीं होती तब तक इस magic constant में एक अनुगामी स्लेश नहीं होता है।
उदाहरण:
<?php
echo "<h3>Example for __DIR__</h3>";
//print full path of directory where script will be placed
echo __DIR__ . "<br><br>";
//below output will equivalent to above one.
echo dirname(__FILE__) . "<br><br>";
?>
आउटपुट:

4. __FUNCTION__:
यह magic constant फ़ंक्शन नाम देता है, जहां इस constant का उपयोग किया जाता है। यदि यह किसी फ़ंक्शन के बाहर उपयोग किया जाता है तो यह blank हो जाएगा।
उदाहरण:
<?php
echo "<h3>Example for __FUNCTION__</h3>";
//Using magic constant inside function.
function test(){
//print the function name i.e; test.
echo 'The function name is '. __FUNCTION__ . "<br><br>";
}
test();
//Magic constant used outside function gives the blank output.
function test_function(){
echo 'Hie';
}
test_function();
//give the blank output.
echo __FUNCTION__ . "<br><br>";
?>
आउटपुट:

5. __CLASS__:
यह class name लौटाता है, जहां इस magic constant का उपयोग किया जाता है। __CLASS__ constant traits में भी काम करता है।
उदाहरण:
<?php
echo "<h3>Example for __CLASS__</h3>";
class JTP
{
public function __construct() {
;
}
function getClassName(){
//print name of the class JTP.
echo __CLASS__ . "<br><br>";
}
}
$t = new JTP;
$t->getClassName();
//in case of multiple classes
class base
{
function test_first(){
//will always print parent class which is base here.
echo __CLASS__;
}
}
class child extends base
{
public function __construct() {
;
}
}
$t = new child;
$t->test_first();
?>
आउटपुट:

6. __TRAIT__:
यह magic constant, trait name लौटाता है, जहां इसका उपयोग किया जाता है।
उदाहरण:
<?php
echo "<h3>Example for __TRAIT__</h3>";
trait created_trait {
function jtp(){
//will print name of the trait i.e; created_trait
echo __TRAIT__;
}
}
class Company {
use created_trait;
}
$a = new Company;
$a->jtp();
?>
आउटपुट:

7. __METHOD__:
यह उस class method का नाम देता है जिसमें यह magic constant शामिल है। method का नाम उसी रूप में वापस किया जाता है जैसा कि घोषित किया गया था।
उदाहरण:
<?php
echo "<h3>Example for __METHOD__</h3>";
class method {
public function __construct() {
//print method::__construct
echo __METHOD__ . "<br><br>";
}
public function meth_fun(){
//print method::meth_fun
echo __METHOD__;
}
}
$a = new method;
$a->meth_fun();
?>
आउटपुट:

8. __NAMESPACE__:
यह current namespace लौटाता है जहां इसका उपयोग किया जाता है।
उदाहरण:
<?php
echo "<h3>Example for __NAMESPACE__</h3>";
class name {
public function __construct() {
echo 'This line will print on calling namespace.';
}
}
$class_name = __NAMESPACE__ . '\name';
$a = new class_name;
?>
आउटपुट:

9. ClassName::class:
यह magic constant डबल अंडरस्कोर (__) के साथ शुरू और समाप्त नहीं होता है। यह ClassName के पूरी तरह से योग्य नाम देता है। यह namespaced classes के साथ उपयोगी है।
उदाहरण:
<?php
namespace Technical_Portal;
echo "<h3>Example for CLASSNAME::CLASS </h3>";
class hinditutorialspoint {
}
echo hinditutorialspoint::class; //ClassName::class
?>
आउटपुट:
Example for ClassName::class
Technical_Portal\hinditutorialspoint
नोट: याद रखें कि namespace पहले statement या script में किसी भी declare कॉल के बाद होना चाहिए, अन्यथा यह fatal error उत्पन्न करेगा।