Reputation: 6810
Hi I am getting a strange error when I run the following script. It is returning this error message
Parse error: syntax error, unexpected 'new' (T_NEW) in /home/apipetfindr/public_html/sys/v1/google_cloud_storeage_upload.php on line 9
I am wondering why this part of my code is not working:
private $storage = new StorageClient
I know I need to call the new function I have tried public instead of private with no success. if anyone can point me in the right direction that would be great.
<?php
# Includes the autoloader for libraries installed with composer
require '../vendor/autoload.php';
use Google\Cloud\Storage\StorageClient;
class googleupload{
private $storage = new StorageClient([
'projectId' => 'xxxxxxxxxx'
]);
private $bucket = $storage->bucket('BUCKET_NAME');
// Upload a file to the bucket.
//$bucket->upload(
// fopen('/data/file.txt', 'r')
//);
public function uploads($_FILE, $uid){
$obj = new Google_Service_Storage_StorageObject();
$obj->setName($file_name);
$resp = $storage->objects->insert(
"bucketname",
$obj,
array('name' => $file_name, 'data' => file_get_contents("300mb-file.zip"), 'uploadType' => 'media')
);
}
}
/*
// Download and store an object from the bucket locally.
$object = $bucket->object('file_backup.txt');
$object->downloadToFile('/data/file_backup.txt');
*/
?>
Upvotes: 1
Views: 378
Reputation: 1048
You are not allowed to use something like new Object(x, y, [z])
as a default value for a class property. If you want to have always the same property-value, e.g. your object with projectId xxxx, you can put something like this within your constructor:
public function __construct(){
$this->storage = new StorageClient(['projectId' => 'xxxxxxxx']);
$this->bucket = $this->storage->bucket('BUCKET_NAME');
}
Upvotes: 1