Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Instantiate amazon AWS inside of a class? 1

Status
Not open for further replies.

ThomasJSmart

Programmer
Sep 16, 2002
634
How do i instantiate AWS inside of a class? All the examples seem to be procedural.

For example, i was thinking something like this:

Code:
<?PHP 
date_default_timezone_set('myTimezone');

// load aws using composer
require_once dirname(__FILE__).'/vendor/autoload.php';

class MyLoader{
  use Aws\Common\Aws;
  protected $aws = NULL;

  public function __construct(){
    $this->aws = Aws::factory(array(
      'key' => 'mykey',
      'secret' => 'mysecret',
      'region' => 'myregion'
    ));
  }
}

and then i could reference $this->aws throughout the class or extends.

but it is telling me this:
Fatal error: MyLoader cannot use Aws\Common\Aws - it is not a trait in MyLoader.php on line 8

Note: i installed aws using composer as recommended by amazon which generated the autoloader file i am using. I have tried different variations of the above but all with errors.
the error on line 8 is referencing the "use" line.

what is the right way to do this to get it working the way i want?

Thanks!
Thomas


site | / blog |
 
use" is a way of aliasing within a namespace.
where have you set up the namespace?

from recollection there was an sdk-class within the aws sdk. that worked simply by instantiation in the normal way.

Code:
// For AWS PHP SDK
require_once 'AWSSDKforPHP/sdk.class.php';
$ec2 = new AmazonEC2();
$response = $ec2->run_instances($ami, 1, 1, array(
    'KeyName' => $keyname,
    'InstanceType' => $instancetype,
    'SecurityGroupId' => $securitygroup,
));
 
i think your example is SDK1 where it was indeed simpler although the sdk had many other issues.
Many of those issues are solved in the new sdk2 but they seem to have taken much flexibility away from loading and are pushing the composer method. It would also be possible to use the old fashioned way of loading classes but i would probably need to manually follow all the dependencies and include them one by 1 or include a single 6mb phar which is fine for development but not really an option for a production environment.

namespaces are set within the composer loading file by the looks of it but i dont really want to use aws namespace in the rest of my abstracted code. Still somewhat new to this concept too so maybe im just missing something.


site | / blog |
 
yup. i'm not at all convinced that php 'needed' namespaces. it seems to be more of a desire to bring php ever closer to c++ rather than a genuine desire for needed functionality.

'use' in this context means that you want to use a 'trait'. traits look rather like a class. think of them as a kind of fine-grained inheritance. personally i'm not sure what they add to life over normal inheritance... although I guess traits could be seen as the inverse of inheritance. aaah- my head hurts.

rather than using 'use' (and there has to be better grammar available for that ...)

anyway, let's try without using a trait and see what happens. if nothing else then a new error message would help triangulate the hunt for a solution a bit.

Code:
public function __construct(){
    $this->aws = Aws\Common\Aws::factory(array(
      'key' => 'mykey',
      'secret' => 'mysecret',
      'region' => 'myregion'
    ));
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top