Google Cloud PHP Client Libraries are releasing new major versions (v2) to
introduce new surface changes.
The PHP Team at Google has developed a tool to automatically upgrade clients
(see ClientUpgradeFixer).
WHAT are the new Cloud Clients?
The new Cloud Clients are in the namespace \Client\, whereas the previous
clients are one directory above with the same name. For example:
// This is the "new" client
use Google\Cloud\PolicyTroubleshooter\V1\Client\IamCheckerClient;
// This is the deprecated client (marked with @deprecated)
use Google\Cloud\PolicyTroubleshooter\V1\IamCheckerClient;
The main difference is that RPC methods which used to take a varying number of
required arguments plus an array of optional arguments, now only take a
singleRequest object:
// Create a client.
$iamCheckerClient = new IamCheckerClient();
// Prepare the request message.
$request = new TroubleshootIamPolicyRequest();
// Call the API and handle any network failures.
try {
/** @var TroubleshootIamPolicyResponse $response */
$response = $iamCheckerClient->troubleshootIamPolicy($request);
printf('Response data: %s' . PHP_EOL, $response->serializeToJsonString());
} catch (ApiException $ex) {
printf('Call failed with message: %s' . PHP_EOL, $ex->getMessage());
}
RPCs use CallOptions
The new surface RPC methods take an optional array of
CallOptions as the second argument. These are similar to how
the $optionalArgs were used in the previous surface, but the new CallOptionsonly contain options for the call itself, whereas the previous $optionalArgs
also held the optional fields for the request body:
// To set call-time options, such as headers, timeouts, and retry options,
// pass them in as the second argument
$callOptions = ['timeoutMillis' => 20];
$response = $iamCheckerClient->troubleshootIamPolicy($request, $callOptions);
Requests have static ::build methods
Using Request objects directly can make it more difficult to quickly draft out
the necessary code to deliver an RPC. To mitigate this friction, a static
::build method is now generated when one or more
method signature annotations
exist on the RPC.
Any request which has recommended parameters defined in its proto will include a
::build method, so that these parameters are easily discoverable:
// Create the RPC request using the static "::build" method
$request = DeletePosixAccountRequest::build($name);
$response = $iamCheckerClient->troubleshootIamPolicy($request);
HOW should I upgrade?
The changes are mostly straightforward, and at a minimum require the following:
Update Google Cloud clients to use the new client namespace by appending
\Client to the existing namespace.
Update RPC calls to accept the corresponding Request object.
NOTE: Client streaming calls do not require a Request object.
Automatically upgrade code using the ClientUpgradeFixer tool
To help migrate code to the new client surface, we've written a
ClientUpgradeFixer to scan code and upgrade it to match
the new client surface. This tool is not guaranteed to work, so be sure to test
everything that it changes thoroughly. Read more about how to install and run
the tool in its README.
# run the CS fixer for that directory using the config above
vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.google.php --dry-run --diff /path/to/my/project
This will output a diff of the changes. Remove --dry-run from the above
command to apply the changes automatically.
-use Google\Cloud\Dlp\V2\DlpServiceClient;
+use Google\Cloud\Dlp\V2\Client\DlpServiceClient;
+use Google\Cloud\Dlp\V2\CreateDlpJobRequest;
use Google\Cloud\Dlp\V2\InspectConfig;
use Google\Cloud\Dlp\V2\InspectJobConfig;
use Google\Cloud\Dlp\V2\Likelihood;
+use Google\Cloud\Dlp\V2\ListInfoTypesRequest;
use Google\Cloud\Dlp\V2\StorageConfig;
// Instantiate a client.
$dlp = new DlpServiceClient();
// optional args array (variable)
-$infoTypes = $dlp->listInfoTypes($parent);
+$request = (new ListInfoTypesRequest());
+$infoTypes = $dlp->listInfoTypes($request);
// optional args array (inline array)
-$job = $dlp->createDlpJob($parent, ['jobId' => 'abc', 'locationId' => 'def']);
+$request2 = (new CreateDlpJobRequest())
+ ->setParent($parent)
+ ->setJobId('abc')
+ ->setLocationId('def');
+$job = $dlp->createDlpJob($request2);
Feedback
Your feedback is important to us! Please continue to provide us with any
thoughts and questions in the Issues section of this
repository.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-07 UTC."],[],[],null,["Version latestkeyboard_arrow_down\n\n- [2.0.5 (latest)](/php/docs/reference/cloud-policy-troubleshooter/latest/migrating)\n- [2.0.4](/php/docs/reference/cloud-policy-troubleshooter/2.0.4/migrating)\n- [1.3.4](/php/docs/reference/cloud-policy-troubleshooter/1.3.4/migrating)\n- [1.2.1](/php/docs/reference/cloud-policy-troubleshooter/1.2.1/migrating)\n- [1.1.2](/php/docs/reference/cloud-policy-troubleshooter/1.1.2/migrating)\n- [1.0.4](/php/docs/reference/cloud-policy-troubleshooter/1.0.4/migrating) \n\nMigrating Google Cloud Clients to the New Surface\n=================================================\n\n**Document Summary**\n\n- Google Cloud PHP Client Libraries are releasing new major versions (v2) to introduce new surface changes.\n- The PHP Team at Google has developed a tool to automatically upgrade clients (see [`ClientUpgradeFixer`](https://github.com/GoogleCloudPlatform/php-tools/blob/main/src/Fixers/ClientUpgradeFixer/README.md)).\n\nWHAT are the new Cloud Clients?\n-------------------------------\n\nThe new Cloud Clients are in the namespace `\\Client\\`, whereas the previous\nclients are one directory above with the same name. For example: \n\n // This is the \"new\" client\n use Google\\Cloud\\PolicyTroubleshooter\\V1\\Client\\IamCheckerClient;\n\n // This is the deprecated client (marked with @deprecated)\n use Google\\Cloud\\PolicyTroubleshooter\\V1\\IamCheckerClient;\n\nThe main difference is that RPC methods which used to take a varying number of\nrequired arguments plus an array of optional arguments, now only take a\n*single* `Request` object: \n\n // Create a client.\n $iamCheckerClient = new IamCheckerClient();\n\n // Prepare the request message.\n $request = new TroubleshootIamPolicyRequest();\n\n // Call the API and handle any network failures.\n try {\n /** @var TroubleshootIamPolicyResponse $response */\n $response = $iamCheckerClient-\u003etroubleshootIamPolicy($request);\n printf('Response data: %s' . PHP_EOL, $response-\u003eserializeToJsonString());\n } catch (ApiException $ex) {\n printf('Call failed with message: %s' . PHP_EOL, $ex-\u003egetMessage());\n }\n\n### RPCs use CallOptions\n\nThe new surface RPC methods take an optional array of\n[CallOptions](https://github.com/googleapis/gax-php/blob/main/src/Options/CallOptions.php) as the second argument. These are similar to how\nthe `$optionalArgs` were used in the previous surface, but the new `CallOptions`\n*only* contain options for the call itself, whereas the previous `$optionalArgs`\nalso held the optional fields for the request body: \n\n // To set call-time options, such as headers, timeouts, and retry options,\n // pass them in as the second argument\n $callOptions = ['timeoutMillis' =\u003e 20];\n $response = $iamCheckerClient-\u003etroubleshootIamPolicy($request, $callOptions);\n\n### Requests have static `::build` methods\n\nUsing Request objects directly can make it more difficult to quickly draft out\nthe necessary code to deliver an RPC. To mitigate this friction, a static\n`::build` method is now generated when one or more\n[method signature annotations](https://google.aip.dev/client-libraries/4232)\nexist on the RPC.\n\nAny request which has recommended parameters defined in its proto will include a\n`::build` method, so that these parameters are easily discoverable: \n\n // Create the RPC request using the static \"::build\" method\n $request = DeletePosixAccountRequest::build($name);\n $response = $iamCheckerClient-\u003etroubleshootIamPolicy($request);\n\nHOW should I upgrade?\n---------------------\n\nThe changes are mostly straightforward, and at a minimum require the following:\n\n- Update Google Cloud clients to use the new client namespace by appending `\\Client` to the existing namespace.\n- Update RPC calls to accept the corresponding `Request` object.\n\n**NOTE** : Client streaming calls do not require a `Request` object.\n\n### Automatically upgrade code using the `ClientUpgradeFixer` tool\n\nTo help migrate code to the new client surface, we've written a\n[ClientUpgradeFixer](https://github.com/GoogleCloudPlatform/php-tools/blob/main/src/Fixers/ClientUpgradeFixer/README.md) to scan code and upgrade it to match\nthe new client surface. This tool is not guaranteed to work, so be sure to test\neverything that it changes thoroughly. Read more about how to install and run\nthe tool in its [README](https://github.com/GoogleCloudPlatform/php-tools/blob/main/src/Fixers/ClientUpgradeFixer/README.md).\n\nThe ClientUpgradeFixer uses [PHP Coding Standards Fixer](https://cs.symfony.com/) to upgrade\ncode to use the new client surface: \n\n # run the CS fixer for that directory using the config above\n vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.google.php --dry-run --diff /path/to/my/project\n\nThis will output a diff of the changes. Remove `--dry-run` from the above\ncommand to apply the changes automatically. \n\n -use Google\\Cloud\\Dlp\\V2\\DlpServiceClient;\n +use Google\\Cloud\\Dlp\\V2\\Client\\DlpServiceClient;\n +use Google\\Cloud\\Dlp\\V2\\CreateDlpJobRequest;\n use Google\\Cloud\\Dlp\\V2\\InspectConfig;\n use Google\\Cloud\\Dlp\\V2\\InspectJobConfig;\n use Google\\Cloud\\Dlp\\V2\\Likelihood;\n +use Google\\Cloud\\Dlp\\V2\\ListInfoTypesRequest;\n use Google\\Cloud\\Dlp\\V2\\StorageConfig;\n\n // Instantiate a client.\n $dlp = new DlpServiceClient();\n\n // optional args array (variable)\n -$infoTypes = $dlp-\u003elistInfoTypes($parent);\n +$request = (new ListInfoTypesRequest());\n +$infoTypes = $dlp-\u003elistInfoTypes($request);\n\n // optional args array (inline array)\n -$job = $dlp-\u003ecreateDlpJob($parent, ['jobId' =\u003e 'abc', 'locationId' =\u003e 'def']);\n +$request2 = (new CreateDlpJobRequest())\n + -\u003esetParent($parent)\n + -\u003esetJobId('abc')\n + -\u003esetLocationId('def');\n +$job = $dlp-\u003ecreateDlpJob($request2);\n\nFeedback\n--------\n\nYour feedback is important to us! Please continue to provide us with any\nthoughts and questions in the [Issues](https://github.com/googleapis/google-cloud-php/issues) section of this\nrepository."]]