[英]PHP - How to call a an external name space within a class method?
I'm using AWS's SDK that requires to use the following namespaces in my code:我正在使用 AWS 的 SDK,它需要在我的代码中使用以下命名空间:
use Aws\Ses\SesClient;
use Aws\Exception\AwsException;
I would like to call these namespaces in my class method to look as follows:我想在我的 class 方法中调用这些命名空间,如下所示:
class EmailManager {
public function sendEmail() {
//new Aws\Ses\SesClient;
use Aws\Ses\SesClient;
use Aws\Exception\AwsException;
However, I get the following error when I call the namespaces Parse error: syntax error, unexpected 'use'
但是,当我调用命名空间Parse error: syntax error, unexpected 'use'
This is what I tried so far and worked.这是我迄今为止尝试过的并且工作过的。 However, I want to be able to call it in my method:但是,我希望能够在我的方法中调用它:
use Aws\Ses\SesClient;
use Aws\Exception\AwsException;
class EmailManager {
How can I call this namespaces in my class method?如何在我的 class 方法中调用此命名空间?
Thank you谢谢
you should insert use
outside the Class, and call the imported class by name: SesClient
not the full namespace.您应该在 Class 之外插入use
,并按名称调用导入的 class: SesClient
而不是完整的命名空间。 Or don't put use
at all and call by the full namespace every time.或者根本不use
,每次都通过完整的命名空间调用。
see documentation:见文档:
AWS SDK for PHP 适用于 PHP 的 AWS SDK
Using namespaces: Aliasing/Importing 使用命名空间:别名/导入
with use
: use
:
require 'vendor/autoload.php';
use Aws\Ses\SesClient;
use Aws\Exception\AwsException;
class EmailManager {
public function sendEmail() {
$SesClient = new SesClient([
'profile' => 'default',
'version' => '2010-12-01',
'region' => 'us-east-2'
]);
without use
:不use
:
class EmailManager {
public function sendEmail() {
$SesClient = new \Aws\Ses\SesClient([
'profile' => 'default',
'version' => '2010-12-01',
'region' => 'us-east-2'
]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.