Home  >  Article  >  Backend Development  >  Android programmers learn PHP development (30)-ThinkPHP5.0 (2) namespace-PhpStorm

Android programmers learn PHP development (30)-ThinkPHP5.0 (2) namespace-PhpStorm

黄舟
黄舟Original
2017-03-03 10:03:441134browse

Namespace uses scope access method

## *

Namespace

Broadly speaking, namespace is a way of encapsulating things. This abstract concept can be found in many places. For example, in the operating system, directories are used to group related files.

For the files in the directory, it plays the role of a namespace. For example, the file foo.txt can exist in the directories /home/greg and /home/other at the same time

, but two foo.txt cannot exist in the same directory. document. In addition, when accessing the foo.txt file outside the directory /home/greg, we must put the directory name and directory separator before the file name to get /home/greg/foo .txt. The application of this principle to the field of programming is the concept of namespace.

http://php.net/manual/zh/language.namespaces.php

## *

ThinkPHP5 adopts namespace method to define and automatically load class library files, which effectively solves the namespace conflict problem between multiple modules and Composer class library, and achieves a more

Efficient automatic loading mechanism of class libraries.

## http://www.kancloud.cn/manual/thinkphp5/118014

*

function applies to namespace

const applies to namespace

class applies to namespace

define is not applicable to namespace

*

Access method:

1 , Unqualified name access method

2. Fully qualified name access method

3. Qualified name access method (relative path)

<?php
    /**
     * 命名空间 使用范围 访问方式
     *
     * 命名空间
     * 从广义上来说,命名空间是一种封装事物的方法。在很多地方都可以见到这种抽象概念。例如,在操作系统中目录用来将相关文件分组,
     * 对于目录中的文件来说,它就扮演了命名空间的角色。具体举个例子,文件 foo.txt 可以同时在目录/home/greg 和 /home/other 中
     * 存在,但在同一个目录中不能存在两个 foo.txt 文件。另外,在目录 /home/greg 外访问 foo.txt 文件时,我们必须将目录名以及目
     * 录分隔符放在文件名之前得到 /home/greg/foo.txt。这个原理应用到程序设计领域就是命名空间的概念。
     * http://www.php.cn/
     *
     * ThinkPHP5采用命名空间方式定义和自动加载类库文件,有效的解决了多模块和Composer类库之间的命名空间冲突问题,并且实现了更加
     * 高效的类库自动加载机制。
     * http://www.php.cn/
     *
     * function 适用于命名空间
     * const 适用于命名空间
     * class 适用于命名空间
     * define 不适用于命名空间
     *
     * 访问方式:
     * 1、非限定名称访问方式
     * 2、完全限定名称访问方式
     * 3、限定名称访问方式(相对路径)
     */


    /**
     * kj1,命名空间1
     */
    namespace kj1;
    function getmsg(){
        echo &#39;123<br>&#39;;
    }
    //define(&#39;MN&#39;,&#39;iwanghang&#39;);
    const MN="iwanghang<br>";
    class Animals{
        public $obj=&#39;cat<br>&#39;;
    }


    /**
     * kj2,命名空间2
     */
    namespace kj2;
    use kj1\Animals;

    function getmsg(){
        echo &#39;456<br>&#39;;
    }
    //define(&#39;MN&#39;,&#39;iwanghang0&#39;);
    const MN="iwanghang0<br>";

    /**
     * 访问方式:1、非限定名称访问方式
     */
    getmsg();

    /**
     * 访问方式:2、完全限定名称访问方式
     */
    \kj1\getmsg();
    \kj2\getmsg();
    echo \kj1\MN;

    $animal = new Animals();
    echo $animal->obj;


    /**
     * 访问方式:3、限定名称访问方式
     */
    //echo kj1\getmsg(); // 会报错,function kj2\kj1\getmsg() 找不到这个函数

    /**
     * 访问方式:3、限定名称访问方式
     */
    namespace kj3;
    echo kj4\getmsg(); // 打印结果:789


    namespace kj3\kj4;
    function getmsg(){
        echo &#39;789<br>&#39;;
    }

The above is the content of Android programmers learning PHP development (30)-ThinkPHP5.0 (2) namespace-PhpStorm. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

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