我有下面的代碼。當我運行它,我得到錯誤:PHP從兩個不同的文件重新聲明類
Fatal error: Cannot redeclare class Google_Account in
/var/www/vhosts/example.com/httpdocs/google-api-php-client
/src/contrib/Google_AnalyticsService.php on line 379
這是因爲無論是「Google_AdsenseService.php」和「Google_AnalyticsService.php」文件中有一個名爲類Google_Account
。 Google_Account類的成員變量和函數在那些文件中是不同的。
我需要同時獲取AdSense和Google Analytics數據。所以我需要同時使用這兩種服務。我無法找到解除類聲明的方法。我怎樣才能一起使用這兩種服務?
include_once APP.'Vendor/google-api-php-client/src/Google_Client.php';
$client1 = new Google_Client();
$client1->setApplicationName('aaa');
$client1->setDeveloperKey('1234');
$client1->setRedirectUri('http://example.com/');
include_once APP.'Vendor/google-api-php-client/src/contrib/Google_AdsenseService.php';
$client1->setClientId('2345');
$client1->setClientSecret('4444');
$service1 = new Google_AdsenseService($client1);
// some code that gets data from "$service1"
$client2 = new Google_Client();
$client2->setApplicationName('aaa');
$client2->setDeveloperKey('1234');
$client2->setRedirectUri('http://example.com/');
include_once APP.'Vendor/google-api-php-client/src/contrib/Google_AnalyticsService.php';
$client2->setClientId('4567');
$client2->setClientSecret('5555');
$service2 = new Google_AnalyticsService($client2);
// some code that gets data from "$service2"
[Namespacing](http://php.net/manual/en/language.namespaces.php)? –
Someone http://stackoverflow.com/questions/1572159/undeclare-a-class-in-php說,這是不可能的un-declare classes – samayo
這是不可能從內部註冊表中刪除類。這就是爲什麼你遵循[psr-0](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md) –