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
andprivate
) - 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
, userequire_once
if you have to. Usingrequire
can cause errors when executing the same block of code twice: redeclaring functions/classes. If time is of the essence, you could opt forrequire
(asrequire_once
causes more overhead), but you have to know what you're doing. - don't
require
an autoloader, usespl_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;
}
}