← Back to PHP Course | Chapter 14: Advanced PHP | Lesson 18 of 24

PHP Magic Constants

Magic constants special built-in names हैं जो आप उन्हें कहाँ इस्तेमाल करते हैं इसके आधार पर बदलते हैं, हमेशा current room दिखाने वाले एक badge जैसे। वे आपको current line, file, या function बता सकते हैं।
Syntax
php
__LINE__;       // current line number
__FILE__;       // full path of this file
__DIR__;        // directory of this file
__FUNCTION__;   // current function name
__METHOD__;     // Class::method

Magic Constants क्या हैं?

Magic constants special identifiers हैं, हमेशा __LINE__ जैसे double underscores में wrapped, जिनकी value code में exactly कहाँ लिखे गए हैं इसके आधार पर बदलती है।

define() से एक बार define किए गए एक regular constant के उलट, PHP हर जगह जहाँ यह दिखे वहाँ एक magic constant को fresh resolve करता है।

उदाहरण: What Are Magic Constants?

php
<?php
// Print `__LINE__` to the output
echo __LINE__;
?>

__LINE__, __FILE__ और __DIR__

__LINE__ current line number देता है और __FILE__ execute हो रही file का full path, जो साथ मिलकर quick debug logging की backbone हैं।

__DIR__ सिर्फ containing directory देता है — आमतौर पर reliable include paths बनाने के लिए इस्तेमाल होता है जो current working directory पर depend न करें।

उदाहरण: __LINE__, __FILE__ and __DIR__

php
<?php
// Print `__LINE__ . "\n"` to the output
echo __LINE__ . "\n";
// Print `__FILE__ . "\n"` to the output
echo __FILE__ . "\n";
// Print `__DIR__` to the output
echo __DIR__;
?>

__FUNCTION__ और __METHOD__

__FUNCTION__ उस function का नाम return करता है जिसके अंदर यह लिखा है, जबकि __METHOD__ class-qualified form, ClassName::methodName, return करता है, जब किसी class method के अंदर इस्तेमाल हो।

यह distinction logging के लिए मायने रखता है, क्योंकि __METHOD__ तुरंत बताता है कि log line किस class से आई।

उदाहरण: __FUNCTION__ and __METHOD__

php
<?php
// Define the function `greet` with no parameters
function greet() {
    // Print `__FUNCTION__ . "\n"` to the output
    echo __FUNCTION__ . "\n";
}
// Define the class `Greeter`
class Greeter {
    // Define the function `hello` with no parameters
    function hello() {
        // Print `__METHOD__` to the output
        echo __METHOD__;
    }
}
// Call `greet()`
greet();
(new Greeter())->hello();
?>

__CLASS__ और __NAMESPACE__

__CLASS__ उस class का fully-qualified name देता है जिसमें यह लिखा है — किसी trait या parent class के अंदर उपयोगी जहाँ आप actual instantiated class चाहते हों, वह नहीं जिसमें code physically defined है।

__NAMESPACE__ current namespace return करता है, namespace-relative class names बनाने के लिए handy।

उदाहरण: __CLASS__ and __NAMESPACE__

php
<?php
// Declare the namespace `App`
namespace App;

// Define the class `Greeter`
class Greeter {
    // Define the function `whoAmI` with no parameters
    function whoAmI() {
        // Print `__CLASS__ . " in " . __NAMESPACE__` to the output
        echo __CLASS__ . " in " . __NAMESPACE__;
    }
}
(new Greeter())->whoAmI();
?>

Debugging और Logging में Practical Uses

कई magic constants को एक log message में combine करना — file, line, और function — आपको बिना कुछ hardcode किए हर log entry के लिए एक exact source location देता है, इसलिए file rename होने या function move होने के बाद भी log accurate रहता है।

उदाहरण: Practical Uses in Debugging and Logging

php
<?php
// Define the function `logMessage` taking `$msg`
function logMessage($msg) {
    // Print "[" . __FILE__ . ":" . __LINE__ . "] " . __FUNCTION__ . ": $msg" to the output
    echo "[" . __FILE__ . ":" . __LINE__ . "] " . __FUNCTION__ . ": $msg";
}
// Call `logMessage("Something happened")`
logMessage("Something happened");
?>
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. यह उम्मीद करना कि __FILE__ एक relative path देगा, जबकि यह file का full path return करता है।
  2. __CLASS__ को किसी class के बाहर इस्तेमाल करना, जहाँ यह एक empty string देता है।
  3. __DIR__ (current file के folder) को current working directory से confuse करना।

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.