如何在类或函数中使用命名空间别名 (use)

How to use a namespace alias (use) inside a class or function

提问人:ALEX 提问时间:12/15/2022 最后编辑:marc_sALEX 更新时间:1/12/2023 访问量:120

问:

在我的脚本中,我有两个名为“Exception”的类。

通常,我会使用

use PHPMailer\PHPMailer\Exception;

然后,使用指向 PHPMailer 的类 Exception。

问题是我不能在函数中使用“use”命令。那么,如何声明每次都使用什么例外呢?

例:

use PHPMailer\PHPMailer\PHPMailer;
use PHPMAiler\PHPMailer\Exceptions;
use PHPMailer\PHPMailer\SMTP;

class email {
  function send_with_smtp {
     $a = new PHPMailer();  // inside this class it needs the exceptions to be used as a PHPMailer\Exceptions
  }

  function send_with_critsend{
     $a = new MXM(); // inside this class it needs the exceptions to be used NOT as a PHPMailer\Exceptions
  }
}

我会做的 - 但不能,因为仅在上层使用使用——如下(这当然是错误的)。但这就是我想实现的目标:

class email {
  function send_with_smtp {
     use PHPMailer\PHPMailer\PHPMailer;
     use PHPMAiler\PHPMailer\Exceptions;
     use PHPMailer\PHPMailer\SMTP;

     $a = new PHPMailer();  // inside this class it needs the exceptions to be used as a PHPMailer\Exceptions
  }

  function send_with_critsend{
     use ANOTHER; // maybe the default Exceptions or something else.
     $a = new MXM(); // inside this class it needs the exceptions to be used NOT as a PHPMailer\Exceptions
  }
}
PHP 命名空间

评论

0赞 Foobar 12/16/2022
正如我之前提到的:不要担心其他库正在使用的异常。只需使用global(在当前文件的开头),当您在该文件中执行操作时。仅当您想自己例外时,您才必须通过 包含它。但所有这些都可以在文件中全局完成,也可以通过完全限定的命名空间完成。use PHPMailer\PHPMailer\PHPMailer;new PHPMailer();throwuse PHPMAiler\PHPMailer\Exception

答:

1赞 outlaw 12/15/2022 #1

您不会只通过其全名引用所需的异常吗?

class alpha {
  function alphafunction {
     $a = new \PHPMailer\Exception\Exception();
  }
}

class beta {
  function betafunction {
     $a = new \AnotherClass\Exception\Exception();
  }
}

评论

0赞 ALEX 12/16/2022
不可以,因为 alphafunction 和 betafunction 调用了另一个类,该类需要使用已在自己的路径中声明的异常
-1赞 Gaurav Negi 12/15/2022 #2

若要在类或函数中使用命名空间别名(也称为“using 指令”),可以将 using 关键字后跟命名空间和要使用的别名放在类或函数定义的顶部。例如:

namespace MyNamespace
{
    class MyClass
    {
        // Use the alias "alias" for the namespace "MyNamespace::MyOtherNamespace"
        using alias = MyNamespace::MyOtherNamespace;

        void MyFunction()
        {
            // Use the alias to access a class or function in the namespace
            alias::MyClass instance;
            instance.DoSomething();
        }
    }
}

在此示例中,using 指令为 MyNamespace 命名空间内的 MyOtherNamespace 命名空间创建别名。这允许您使用别名访问 MyOtherNamespace 命名空间中的类或函数,而无需完全限定其名称。

评论

1赞 iBug 12/16/2022
这个答案看起来像是由 ChatGPT 生成的。请注意,ChatGPT 目前已被禁止
0赞 Moudi 12/19/2022
这个代码片段很笼统,不适用这个问题,ChatGPT 是你吗?
0赞 Foobar 12/16/2022 #3

一方面,您可以对使用不同异常的不同类使用不同的文件。

阿尔法 .php

use PHPMailer\PHPMailer\Exception;
class alpha {
  function alphafunction {
     throw new Exception();
  }
}

测试版:.php

use AnotherClass\Exception\Exception;
class beta{
  function alphafunction {
     throw new Exception();
  }
}

但你也可以在一个文件中做:

use PHPMailer\PHPMailer\Exception as PHPMailerException ;
use AnotherClass\Exception\Exception as AnotherClassException;

class alpha {
  function alphafunction {
     throw new PHPMailerException();
  }
}

class beta {
  function betafunction {
     throw new AnotherClassException();
  }
}

主题:命名空间别名

如果你想知道你从其他地方得到什么例外,请执行以下操作:

try {
  //do stuff here
} catch(\Exception $e){
  print get_class($e);
}

评论

0赞 ALEX 12/16/2022
“不同类的不同文件”是什么意思?问题是我必须使用“异常”,因为它被其他库使用。我的意思是 alpha 类调用一些其他函数和类。
0赞 Foobar 12/16/2022
@ALEX行,仅在 currend 文件中使用。我稍微更新了一下我的答案。use X as Y;
0赞 ALEX 12/16/2022
我需要使用与其他库相同的“异常”。phpmailer 需要自己的异常,其他库也需要自己的异常。如果我可以在函数中使用“use”就可以解决问题,但我不能,因为它需要在上层。
0赞 Foobar 12/16/2022
@ALEX 你可以忽略它。因为命名空间是基于文件的,并且当前文件中的任何内容都仅与此文件相关。您可以并在其他文件中。没有冲突。alpha class call some other functions and classesuseuse foo/bar as foobar;use foo/bar as bazz;
0赞 Foobar 12/16/2022
@ALEX函数中“使用”--> 然后在函数中使用完全限定的命名空间,例如 .throw new \PHPMailer\PHPMailer\Exception();