栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP

PHP魔术常量

PHP 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

文件名: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; ?>

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



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/9902.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号