【问题标题】:How to use a namespace alias (use) inside a class or function如何在类或函数中使用命名空间别名(使用)
【发布时间】:2022-12-16 08:34:26
【问题描述】:

在我的脚本中,我有两个名为“Exception”的类。 通常,我会使用

use PHPMailer\PHPMailer\Exception;

然后,使用指向 PHPMailer 的异常类。

问题是我不能在函数内使用“use”命令。那么我如何声明每次使用什么异常呢?

例子:

class alpha {
  function alphafunction {
     $a = new Exception(); // Here I want to use PHPMailer\Exception
  }
}

class beta {
  function betafunction {
     $a = new Exception(); // Here I want to use AnotherClass\Exception
  }
}

我该怎么做,因为必须在上层声明使用?

谢谢, 亚历克斯

我想在不同的函数或类中声明不同的类。

【问题讨论】:

    标签: php class namespaces


    【解决方案1】:

    难道您不只是通过全名来引用所需的异常吗?

    class alpha {
      function alphafunction {
         $a = new PHPMailerExceptionException();
      }
    }
    
    class beta {
      function betafunction {
         $a = new AnotherClassExceptionException();
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-16
      • 2013-04-13
      • 1970-01-01
      • 1970-01-01
      • 2015-03-15
      • 2015-07-28
      相关资源
      最近更新 更多