PHP魔术常量

PHP魔术常量,第1张

PHP魔术常量

下面来看看一个上面的每个魔法常量的例子。

文件名magic.php


";// print Your current line number i.e;3 echo "Example for __FILE__"; echo __FILE__ . "

";//print full path of file with .php extension echo "Example for __DIR__"; echo __DIR__ . "

";//print full path of directory where script will be placed echo dirname(__FILE__) . "

"; //its output is equivalent to above one. echo "Example for __FUNCTION__"; //Using magic constant inside function. function cash(){ echo 'the function name is '. __FUNCTION__ . "

";//the function name is cash. } cash(); //Using magic constant outside function gives the blank output. function test_function(){ echo 'HYIIII'; } test_function(); echo __FUNCTION__ . "

";//gives the blank output. echo "Example for __CLASS__"; class abc { public function __construct() { ; } function abc_method(){ echo __CLASS__ . "

";//print name of the class abc. } } $t = new abc; $t->abc_method(); class first{ function test_first(){ echo __CLASS__;//will always print parent class which is first here. } } class second extends first { public function __construct() { ; } } $t = new second; $t->test_first(); echo "Example for __TRAIT__"; trait created_trait{ function abc(){ echo __TRAIT__;//will print name of the trait created_trait } } class anew{ use created_trait; } $a = new anew; $a->abc(); echo "Example for __METHOD__"; class meth{ public function __construct() { echo __METHOD__ . "

";//print meth::__construct } public function meth_fun(){ echo __METHOD__;//print meth::meth_fun } } $a = new meth; $a->meth_fun(); echo "Example for __NAMESPACE__"; class name{ public function __construct() { echo 'This line will be printed on calling namespace'; } } $clas_name= __NAMESPACE__ .'name'; $a = new $clas_name; ?>

执行上面代码得到以下结果 -



欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/3012384.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-09-28
下一篇 2022-09-28

发表评论

登录后才能评论

评论列表(0条)