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

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

不言
Release: 2023-04-04 10:52:01
forward
2893 people have browsed it

本篇文章给大家带来的内容是关于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();
}
?>
Copy after login

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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template