PHP Magic Constants
In this page:
__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
// Print `__LINE__` to the output
echo __LINE__;
?>
Login to try C/C++/Java/PHP code in the editor
__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
// 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__;
?>
Login to try C/C++/Java/PHP code in the editor
__FUNCTION__ और __METHOD__
__FUNCTION__ उस function का नाम return करता है जिसके अंदर यह लिखा है, जबकि __METHOD__ class-qualified form, ClassName::methodName, return करता है, जब किसी class method के अंदर इस्तेमाल हो।
यह distinction logging के लिए मायने रखता है, क्योंकि __METHOD__ तुरंत बताता है कि log line किस class से आई।
उदाहरण: __FUNCTION__ and __METHOD__
<?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();
?>
Login to try C/C++/Java/PHP code in the editor
__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
// 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();
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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");
?>
Login to try C/C++/Java/PHP code in the editor
- यह उम्मीद करना कि
__FILE__एक relative path देगा, जबकि यह file का full path return करता है। __CLASS__को किसी class के बाहर इस्तेमाल करना, जहाँ यह एक empty string देता है।__DIR__(current file के folder) को current working directory से confuse करना।
Chapter Quiz — Complete all 24 topics to unlock
0/24 topics done
Complete these topics first:
- PHP Date & Time
- PHP Math Functions
- PHP JSON Handling
- PHP XML Handling
- PHP cURL Introduction
- PHP REST API Basics
- PHP Composer & Packages
- PHP Autoloading
- PHP Design Patterns
- PHP MVC Architecture
- PHP Security Best Practices
- PHP Performance Optimization
- PHP 8 New Features
- PHP Type Declarations
- PHP Match Expression Advanced
- PHP Fibers
- PHP Attributes
- PHP Magic Constants
- PHP Include & Require
- PHP Iterables
- PHP SimpleXML Parser
- PHP SimpleXML Get
- PHP XML Expat Parser
- PHP DOM Parser