douou1891 2013-08-29 13:58
浏览 54
已采纳

PHP Amazon Ses作为抽象类会产生使用致命错误

I'm using the Amazon Simple Email Service and am trying to implement it as an abstract class so that I can simply use it throughout as needed.

Problem

The problem occurs with the use, I cannot work out how to require the files and classes needed to use Ses as an abstract class without incurring errors.

require 'lib/aws/aws-autoloader.php';

use Aws\Common\Enum\Region;
use Aws\Ses\SesClient;

abstract class simpleemail {

    function sendSesEmail($to, $subject, $body, $bodyHtml){

        try {   
            $client = SesClient::factory(array(
                'key' => "",
                'secret' => "",
                'region' => Region::US_EAST_1
            ));

            $send = $client->sendEmail(array(
                'Source' => 'Name <no-reply@contact.com>',
                'Destination' => array('ToAddresses' => array($to)),
                'Message' => array('Subject' => array('Data' => $subject), 'Body' => array('Html' => array('Data' => $bodyHtml)))));

            return true;
        }
        catch(Exception $e){
            echo $e->getMessage();
            return false;
        }
    }
}

Error Messages

Fatal error: Class 'Aes\Ses\SesClient' not found in ....

I have tried changing the use to require but then get:

require 'lib/aws/Aws/Common/Enum/Region.php';
require 'lib/aws/Aws/Ses/SesClient.php';

Fatal error: 'SesClient' not found in ...

Solution?

How can I use/require the files I need to get this working inside an abstract class?

  • 写回答

1条回答 默认 最新

  • doumu5934 2013-08-29 14:05
    关注

    This doesn't work:

    abstract class simpleemail
    {
        public function sendSesEmail()
        {
            use Aws\Common\Enum\Region;
            use Aws\Ses\SesClient;
            //...
        }
    }
    

    use statements are, basically, imports, that are processed at compile-time, so they can't be scoped. They have to move to the outer scope (outside of the class).
    If you want to scope them, you'll have to either, manually require them, or use class_alias.
    Check my answer to this question for more details. Even more details can, as ever, be found on php.net

    Side-notes:

    • Please, follow the coding standards as described by PHP-FIG. They're not official, but Zend, Symfony... all major players, in fact, subscribe to them.
    • Please get in the habbit of always specifying the accessmodifiers (public, protected and private)
    • When creating instances like you do $client = SesClient::factory, assign them to a property, to only create the instance once. At the moment, each method call creates the same instance over and over again. That's bad
    • When using properties: include them in the class definition!
    • You're calling sendEmail on an instance, and assign the return value to $send. You don't check the return value, nor do you return it. Either ignore the return value, or return it, so it can be checked!
    • Never use require, use require_once if you have to. Using require can cause errors when executing the same block of code twice: redeclaring functions/classes. If time is of the essence, you could opt for require (as require_once causes more overhead), but you have to know what you're doing.
    • don't require an autoloader, use spl_autoloader_register
    • Remember: Abstract classes can't be instantiated, only their children... children can also override the methods declared in the abstract class. Declare critical methods as final

    So, the answer:

    use Aws\Common\Enum\Region;
    use Aws\Ses\SesClient;
    abstract class simpleemail
    {
        protected $client = null;
        final public function sendSesEmail()
        {
            $client = $this->getClient();//lazy-loads the client instance
            return $client->sendEmail(/* ... */);//return the result
        }
        //lazy-loader
        protected function getClient()
        {
            if ($this->client === null)
            {
                $this->client = SesClient::factory(array(
                    'key' => "",
                    'secret' => "",
                    'region' => Region::US_EAST_1
                ));
            }
            return $this->client;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答