I am trying to write some code that access images from S3 on AWS using inheritance. I have gotten the code to work in one long file but it gets a bit unwieldy.
I am new to PHP inheritance so I'm not sure what's going wrong here. I have a class called PersonPics
that inherits from myAWS
. These are referenced in a third file downloadImages
.
I've traced the error to be related to the $s3Client
variable.
When I have the code written in the myAWS
class written as $this->s3Client->getIterator(...)
I get an error calling member function on null error. If I change it to $this->$s3Client->getIterator(...)
I get a undefined variable s3ClientError
despite it being defined in the constructor.
What am I doing wrong?
myAWS.php
class myAWS{
private $s3Client;
private $bucket = 'my-bucket';
function __construct() {
$S3Client = S3Client::factory(array( ... )); //credentials inserted
}
function getImages($name){
$img_iter = $this->s3Client->getIterator('ListObjects', array(
'Bucket' => $bucket,
'Prefix' => $name . '/img',
));
// ... more
return img_arr;
}
}
PersonPics.php
error_reporting(E_ALL);
ini_set("display_errors","On");
require_once __DIR__ . '/myAWS.php';
class PersonPics extends myAWS {
public $name;
function __construct($name){
$this->$name = $name;
}
function getImages(){
$pic_arr = parent::getImages($this->name);
// more functionality to be added later using $pic_arr
}
}
downloadImages.php
require_once __DIR__ . '/myAWS.php';
require_once __DIR__ . '/PersonPics.php';
$name = $_GET["name"];
$aws = new myAWS();
$lookupPerson = new PersonPics($name);
$images = $lookupPerson->getImages();
In your constructor, you are setting a local variable. You mean to be setting the member variable. Change it to:
function __construct() {
$this->s3Client = S3Client::factory(array( ... )); //credentials inserted
}
Note the change in case of the 's' in the variable name as well.
Replace your myAWS.php class with below code and let me know if it works for you
class myAWS{
private $s3Client;
private $bucket = 'my-bucket';
function __construct() {
$this->s3Client = S3Client::factory(array( ... )); //credentials inserted
}
function getImages($name){
$img_iter = $this->s3Client->getIterator('ListObjects', array(
'Bucket' => $bucket,
'Prefix' => $name . '/img',
));
// ... more
return img_arr;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.