V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
Nicolay
V2EX  ›  PHP

关于命名空间的问题,请教大家。

  •  
  •   Nicolay · Jun 6, 2019 · 3610 views
    This topic created in 2519 days ago, the information mentioned may be changed or developed.
    require_once('./service/Email.class.php');
    $ob = \Service\Email::getInstance();
    Email 类在 Service 命名空间下,为什么写成 $ob = Email::getInstance();会运行失败,不是已经把 email 类引入当前页面了么,为什么还要显式的写上命名空间。
    5 replies    2019-06-09 13:41:09 +08:00
    Nicolay
        1
    Nicolay  
    OP
       Jun 6, 2019
    是在一个没有命名空间的 php 文件上书写如上的代码的。
    ben1024
        2
    ben1024  
       Jun 6, 2019
    因为没有命名空间的 php 文件上书写,Email::getInstance 找不到对应域。
    所有要从根路径查询,
    如果不想这样使用先可以 use Service\Email 声明作用域;类内进行使用。
    curiousjude
        3
    curiousjude  
       Jun 6, 2019
    建议还是要好好去理解一下命名空间是要解决什么问题。

    你给的代码,如果我再同时引入一个命名空间为 \Xxx\Email 的类,你不指定命名空间,那到底是要用哪一个?
    U7Q5tLAex2FI0o0g
        4
    U7Q5tLAex2FI0o0g  
       Jun 6, 2019
    建议先弄懂命名空间
    weirdo
        5
    weirdo  
       Jun 9, 2019
    命名空间是命名空间
    和 include/require 是两码事
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3580 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 43ms · UTC 04:49 · PVG 12:49 · LAX 21:49 · JFK 00:49
    ♥ Do have faith in what you're doing.