Home php教程 PHP源码 PHP namespaces and dynamic language features

PHP namespaces and dynamic language features

Nov 23, 2016 am 10:55 AM
php namespace

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
?>
Copy after login

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

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
?>
Copy after login


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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to handle PHP namespace loading errors and generate related error prompts How to handle PHP namespace loading errors and generate related error prompts Aug 07, 2023 am 11:37 AM

How to handle PHP namespace loading errors and generate related error prompts. In PHP development, namespace is a very important concept. It can help us organize and manage code and avoid naming conflicts. However, when using namespaces, sometimes some loading errors occur. These errors may be caused by incorrect namespace definitions or incorrect paths to loaded files. This article will introduce some common namespace loading errors, give corresponding processing methods, and how to generate relevant error prompts. 1. Namespace determination

Tips for using namespaces in PHP Tips for using namespaces in PHP May 27, 2023 am 11:01 AM

With the development of PHP, the amount of code continues to increase, and the organizational structure of the code becomes more and more important. When using PHP, namespace is a very important concept. It can help us better organize the code, solve problems such as naming conflicts, and improve the readability and maintainability of the code. This article will introduce how to use namespaces in PHP, as well as some common usage tips. 1. Basic concepts of namespaces Namespaces are a way of organizing related classes, interfaces, functions, etc. In PHP, namespaces use the keyword "n

How to use PHP namespaces and FAQs How to use PHP namespaces and FAQs Jun 09, 2023 am 08:15 AM

With the development of the PHP language, the concept of namespace is gradually introduced into PHP. Namespace is a method of organizing code structure to avoid naming conflicts and code confusion. In this article, we will explore how to use PHP namespaces and answer frequently asked questions. 1. Definition of namespace Namespace is a new feature introduced in PHP5.3, which allows developers to better organize their code. A namespace is an identifier (Identifier

PHP namespace error problem and how to solve it PHP namespace error problem and how to solve it May 11, 2023 am 09:10 AM

With the development of modern programming languages, namespaces have become a widely used concept. In the PHP language, namespaces have also been widely used and have become a necessary feature, which can help us avoid naming conflicts, organize code structure, etc. But this also brings about some problems, one of which is the namespace error problem. This article will discuss the PHP namespace error problem and provide solutions. 1. What is a PHP namespace? Before introducing the PHP namespace error problem, we need to first understand

Analyzing namespaces in PHP object-oriented programming Analyzing namespaces in PHP object-oriented programming Aug 10, 2023 pm 02:12 PM

PHP is a very commonly used scripting language that is widely used in web development. As the size of the project increases, the complexity of the code also increases. In order to better manage and organize the code, PHP introduces the concept of namespace. This article will analyze the namespace in PHP object-oriented programming and give corresponding code examples. The concept of namespace A namespace is a mechanism for logically grouping code, similar to the role of a folder (directory) in a file system. It prevents naming conflicts between different classes

How to use namespaces in PHP to manage and operate database-related data types How to use namespaces in PHP to manage and operate database-related data types Jul 15, 2023 pm 02:15 PM

How to use namespaces in PHP to manage and operate database-related data types Introduction: In PHP, database operations are an important part of development. Using namespaces to manage and operate database-related data types can improve code maintainability and readability. This article will introduce how to use namespaces in PHP to manage and operate database-related data types, and provide relevant code examples. 1. The concept of namespace Namespace is a technology used to solve naming conflict problems. By encapsulating functions, classes, interfaces, etc.

Solve PHP error: calling undefined namespace Trait Solve PHP error: calling undefined namespace Trait Aug 21, 2023 pm 06:45 PM

Solve PHP error: calling undefined namespace Trait. During the development process of using PHP, if the error of calling undefined namespace Trait occurs, it is usually caused by namespace-related problems. This article will introduce the cause and solution of this problem, and attach relevant code examples. Cause Analysis In PHP, it is a common practice to use namespaces to organize and manage code. The role of namespace is to avoid naming conflicts and facilitate code maintenance and expansion. When we are using Trait

How to use namespaces in PHP to manage and manipulate custom data types How to use namespaces in PHP to manage and manipulate custom data types Jul 18, 2023 pm 12:00 PM

How to use namespaces in PHP to manage and manipulate custom data types In PHP, namespaces are a mechanism used to resolve naming conflicts and organize code. By using namespaces, we can classify related classes, interfaces, functions, and constants into a specific namespace to achieve better code organization and management. In this article, we will focus on how to use namespaces to manage and manipulate custom data types. We will explain the specific steps in detail through code examples. First, we need to create a named

See all articles