Call to undefined method error when using Singleton in PHP
I have a project where I use aws sd开发者_StackOverflow中文版k for php. Here I am trying to create S3Client with singleton. But it does not create S3Client outside of the constructor.
Here is the class:
class ClientController
{
private static $client;
private function __construct()
{
try {
self::$client = new S3Client([
'region' => AWS_S3_REGION,
'version' => AWS_S3_VERSION,
'credentials' => [
'key' => AWS_S3_KEY,
'secret' => AWS_S3_SECRET_KEY
],
]);
echo 'test3333333333';
} catch (Exception $ex) {
echo $ex->getMessage();
//logging
}
}
public static function s3Client()
{
if(!isset(self::$client)) {
echo 'test1';
self::$client = new ClientController();
}
echo 'test2';
return self::$client;
}
}
And index file I run in another folder:
var_dump(ClientController::s3Client()->getObject(array(
'Bucket' => 'jetdeneme',
'Key' => $request,
)));
Output:
test1test3333333333test2
Fatal error: Uncaught Error: Call to undefined method ClientController::getObject() in C:\xampp\htdocs\cdn\index.php:10
When I use the above var_dump in ClientController's constructor, it doesn't give an error. But it gives outside of the constructor. I just want to create and use the Client using the static function.
精彩评论