search
Homephp教程PHP源码PHP namespaces and dynamic language features

PHP 命名空间的实现受到其语言自身的动态特征的影响。因此,如果要将下面的代码转换到命名空间中:

Example #1 动态访问元素

example1.php:

<?php
    class classname
    {
        function __construct()
        {
            echo __METHOD__,"\n";
        }
    }
    function funcname()
    {
        echo __FUNCTION__,"\n";
    }
    const constname = "global";
    $a = &#39;classname&#39;;
    $obj = new $a; // prints classname::__construct
    $b = &#39;funcname&#39;;
    $b(); // prints funcname
    echo constant(&#39;constname&#39;), "\n"; // prints global
?>

必须使用完全限定名称(包括命名空间前缀的类名称)。注意因为在动态的类名称、函数名称或常量名称中,限定名称和完全限定名称没有区别,因此其前导的反斜杠是不必要的。

Example #2 动态访问命名空间的元素

<?php
    namespace namespacename;
    class classname
    {
        function __construct()
        {
            echo __METHOD__,"\n";
        }
    }
    function funcname()
    {
        echo __FUNCTION__,"\n";
    }
    const constname = "namespaced";
    include &#39;example1.php&#39;;
    $a = &#39;classname&#39;;
    $obj = new $a; // prints classname::__construct
    $b = &#39;funcname&#39;;
    $b(); // prints funcname
    echo constant(&#39;constname&#39;), "\n"; // prints global
    /* 注意如果使用双引号, 一定要使用"\\namespacename\\classname"进行转义 */
    $a = &#39;\namespacename\classname&#39;;
    $obj = new $a; // prints namespacename\classname::__construct
    $a = &#39;namespacename\classname&#39;;
    $obj = new $a; // also prints namespacename\classname::__construct
    $b = &#39;namespacename\funcname&#39;;
    $b(); // prints namespacename\funcname
    $b = &#39;\namespacename\funcname&#39;;
    $b(); // also prints namespacename\funcname
    echo constant(&#39;\namespacename\constname&#39;), "\n"; // prints namespaced
    echo constant(&#39;namespacename\constname&#39;), "\n"; // also prints namespaced
?>


Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools