0

I have the code below. When I run it I get error:

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

That's because both of the "Google_AdsenseService.php" and "Google_AnalyticsService.php" files have a class named Google_Account. The member variables and functions of Google_Account class are different in that files.

I need to get Adsense and Analytics data in the same time. So I need to use both of the services at once. I couldn't find a way to un-declare classes. How can I use both of the services together?

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"
tereško
  • 58,060
  • 25
  • 98
  • 150
trante
  • 33,518
  • 47
  • 192
  • 272
  • [Namespacing](http://php.net/manual/en/language.namespaces.php)? – Waleed Khan Jan 18 '13 at 18:33
  • Someone http://stackoverflow.com/questions/1572159/undeclare-a-class-in-php says, it is impossible to un-declare classes – samayo Jan 18 '13 at 18:35
  • It's not possible to remove classes from the internal registry. This is why you follow [psr-0](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md) – Mike B Jan 18 '13 at 18:36
  • You could use https://github.com/gh0zt/google-api-php-client instead. It's namespaced, but still a work in progress. You could help move it along. Analytics should work (it worked for me :p). – wimvds Jan 18 '13 at 18:43
  • @wimvds thank you for help. but adsense is missing in that fork.. – trante Jan 18 '13 at 19:15

2 Answers2

1

You can add different namespaces at the top of each files in contrib directory. For example for Google_AdsenseService.php file add namespace Google\AdsenseService; at the top.

// Google_AdsenseService.php file
namespace Google\AdsenseService;

As long as the file contents are only referencing the contents from same file it'll will work. Only when you access it you access by namespace. Like this,

$service1 = new Google\AdsenseService\Google_AdsenseService($client1);
Shiplu Mokaddim
  • 56,364
  • 17
  • 141
  • 187
  • Thank you for suggestion but the class implementations are different: http://code.google.com/p/google-api-php-client/source/browse/trunk/src/contrib/Google_AdsenseService.php#912 and http://code.google.com/p/google-api-php-client/source/browse/trunk/src/contrib/Google_AnalyticsService.php#440 – trante Jan 18 '13 at 18:45
0

You have two options:

  1. For PHP 5.3+, you can add a namespace at the start of the file. After this, you need to fix references to other classes from the modified class (Exception will become ::Exception, etc.)

  2. You may rename the class in a text editor, this will be probably easier. Just open up the file in your favorite text editor, and use replace all. Change Google_Client to something else. There is a good change the lib won't use dynamic class construction and other funny stuff, so your quickly refactored code will work.

Jakob Jingleheimer
  • 30,952
  • 27
  • 76
  • 126
Dutow
  • 5,638
  • 1
  • 30
  • 40