PHP Magic Constants In Hindi

📔 : PHP 🔗

Magic Constants, PHP कुछ predefined constants जिनका Output उनके use किये गए place के according change होता रहता हैं। PHP में Magic Constants कुछ इस प्रकार हैं-

Keyword NameUses
__LINE__
Current line number
__FILE__Returns the full file path with file name.
__DIR__Returns the directory of the.
__FUNCTION__Returns the function name or {closure} for anonymous functions.
__CLASS__Returns the class name.
__TRAIT__Returns the trait name that may include namespace.
__METHOD__Returns the method name with class name.
__NAMESPACE__Returns the namespace name
ClassName::classReturns class name with namespace .

PHP __LINE__

__LINE__ PHP constant current line number return करता है , जिस line में यह use हो रहा है।

Example -

<?php
/* Know the line number*/ echo '__LINE__ variable tells the current line number : '.__LINE__; ?>

Output :

__LINE__ variable tells the current line number : 3

Example में आप देख सकते हैं कि __LINE__ current line return करता है।

PHP __FILE__

<?php
/* Know full path of file*/ echo '__FILE__ variable tells the full path : '.__FILE__; ?>

Output :

__FILE__ variable tells the full path : C:\xampp\htdocs\test\test.php

PHP __DIR__

__DIR__ PHP constant current file के साथ full path return करता है।

<?php
/* know the directory name*/ echo '__DIR__ variable tells directory name of current file : '.__DIR__; ?>

Output :

__DIR__ variable tells directory name of current file : C:\xampp\htdocs\test

__FUNCTION__

__FUNCTION__ PHP constant current function का नाम return करता है, जिस function में यह use हो रहा है । हालाँकि __FUNCTION__ की जगह आप __METHOD__ constants  भी use कर सकते हैं।

<?php
/* define the function first*/ function test_fun() { echo '__FUNCTION__variable tells the current function name : '.__FUNCTION__; } // call the function. test_fun(); ?>

Output :

__FUNCTION__variable tells the current function name : test_fun

PHP __CLASS__

__CLASS__ PHP constant current class name return करता है ।

<?php /* define the class*/
class Name { public function __construct() { echo '__CLASS__ tells the class name : '. __CLASS__; } } /* now make object */ new Name(); ?>

Output :

__CLASS__ tells the class name : Name

Note - अगर आप class के बारे में नहीं जानते हैं , तो class के बारे में आप आगे पढ़ेंगे।

__TRAIT__

__TRAIT__PHP constant current trait name return करता है ।

<?php
/*defining trait*/ trait MyTrait { public function trait_fun() { echo "__TRAIT__ tells the trait name : ".__TRAIT__; } } /*define class to access function inside trait*/ class MyClass { /* use trait */ use MyTrait; public function __construct() { /*calling trait function*/ $this->trait_fun(); } } /* making class object */ new MyClass(); ?>

Output :

__TRAIT__ tells the trait name : MyTrait

Understanding Trait - traits , PHP में collection Of functions होते हैं , जिसका सबसे बड़ा फायदा यह है कि हम same name के functions को आसानी से manage कर सकते हैं। बाकी traits के बारे में हम आगे deeply पड़ेंगे।

__METHOD__

__METHOD__ PHP constant current method का नाम return करता है, जिस method में यह use हो रहा है । हालाँकि __METHOD__ की जगह आप __FUNCTION__ constants  भी use कर सकते हैं। इनमे difference यह है कि __METHOD__, class के name के साथ function name return करता है जबकि __FUNCTION__  सिर्फ method का नाम return करता है।

<?php
/* define the class*/ class MyClass { public function myfun() { echo '__METHOD__ tells the method name : '. __METHOD__; } } /* now make object */ $obj = new MyClass(); $obj->myfun(); ?>

Output :

__METHOD__ tells the method name : MyClass::myfun

PHP __NAMESPACE__

__NAMESPACE__ PHP constant current NAMESPACE name return करता है ।

<?php
namespace MyNamespace; class MyClass { public function myfun() { echo '__NAMESPACE__ tells the current namespace : '. __NAMESPACE__; } } /* now make object */ $obj = new MyClass(); $obj->myfun(); ?>

Output :

__NAMESPACE__ tells the current namespace : MyNamespace

PHP ClassName::class

ClassName::class PHP constant current ClassName name return करता है , हालाँकि इसकी जगह आप __CLASS__ भी use कर सकते हैं दोनों ही class का नाम return करते हैं।

<?php
class MyClass { public function myfun() { echo 'MyClass::class tells the current class name : '.MyClass::class; } } /* now make object */ $obj = new MyClass(); $obj->myfun(); ?>

Output :

MyClass::class tells the current class name : MyClass

Understanding Namespace - Namespace , PHP में collection of classes होते हैं , बाकी इनके बारे में आगे हम अच्छी तरह से पढ़ेंगे।

I hope PHP में मौजूद Magic Constants के बारे में आप जान गए होंगे कि किस तरह से इन्हे use करते हैं।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers