xx19941215
V2EX  ›  问与答

PHP use 关键字必须出现在全局作用域?还不能在类或者函数中出现?

  •  
  •   xx19941215 · Aug 4, 2017 · 2877 views
    This topic created in 3209 days ago, the information mentioned may be changed or developed.

    读到 《 Modern PHP 》 17 页 ,有点不理解

    5 replies    2017-08-04 18:09:45 +08:00
    iyaozhen
        1
    iyaozhen  
       Aug 4, 2017 via Android
    放在文件的最上面。

    不能在类或者函数里面需要的时候再 use xxx
    lamCJ
        2
    lamCJ  
       Aug 4, 2017
    - class 中可以 use trait
    - 匿名函数中可以用 use 引入参数
    fuxkcsdn
        3
    fuxkcsdn  
       Aug 4, 2017 via iPhone
    常见的使用场景就是 2 楼说的这 2 种
    在文件开头使用的这种方法还没用过(使用过的开源项目也没见过)
    2ME
        4
    2ME  
       Aug 4, 2017
    @fuxkcsdn .. use 引入包阿 文件开头使用 紧跟在命名空间后 composer 的能获取的包都是这么引入的吧 你要是 github 下载一个个用 include 引入的话就当我没说= =
    fuxkcsdn
        5
    fuxkcsdn  
       Aug 4, 2017 via iPhone
    @2ME 蛤,这个最常用的反而给忘了…
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   969 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 21:52 · PVG 05:52 · LAX 14:52 · JFK 17:52
    ♥ Do have faith in what you're doing.