Home  >  Article  >  Backend Development  >  How to use namespace when php performs dynamic access (code)

How to use namespace when php performs dynamic access (code)

不言
不言forward
2018-10-25 16:40:592853browse

本篇文章给大家带来的内容是关于php进行动态访问时命名空间如何使用(代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

受PHP自身的动态特征的影响,在动态访问元素时,必须使用完全限定名,所以在动态的类、函数或常量名称中,限定名称和完全限定名没有区别,都是按照完全限定名进行解析

<?php
namespace {

const con=0;
const test="test";
function foo(){
    echo &#39;foo in global&#39;."\n";
}
class A{
    static function foo(){
        echo &#39;foo in global\A&#39;."\n";
    }
}
}
namespace a\b{
const con = 1;
const test="a\b\\test";
function foo(){
    echo &#39;foo in a\b\foo&#39;."\n";
}
class A{
    static function foo(){
        echo &#39;foo in a\b\A&#39;."\n";
    }
}


$a=&#39;A&#39;;
$obj=new $a();
$obj->foo();
$a=&#39;\A&#39;;
$obj=new $a();
$obj->foo();
$a=&#39;a\b\A&#39;;
$obj=new $a();
$obj->foo();
$a=&#39;\a\b\A&#39;;
$obj=new $a();
$obj->foo();

$b="con";
echo constant($b)."\n";
$b="\con";
echo constant($b)."\n";
$b="a\b\con";
echo constant($b)."\n";
$b="\a\b\con";
echo constant($b)."\n";

$c="foo";
$c();
$c="\\foo";
$c();
$c="a\b\\foo";
$c();
$c="\\a\\b\\foo";
$c();
}
?>

The above is the detailed content of How to use namespace when php performs dynamic access (code). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete