The PHP client library eases interactions with the Google Ads API with minimal
configuration on your part. However, performance depends significantly on how
the library is used and integrated.

Most [general best practices](https://developers.google.com/google-ads/api/docs/best-practices/overview.md.txt) are applicable to all languages.
This guide covers performance optimizations specific to PHP.

## Protobuf and gRPC implementations

[Protobuf](https://developers.google.com/protocol-buffers/docs/overview) is used by [gRPC](https://grpc.io/about/) and the Google Ads API for
request and response messages. Two implementations are available (pure PHP and a
C extension), and the C extensions for both `protobuf` and `grpc` provide
substantially better serialization and network performance.

See the [Protobuf guide](https://developers.google.com/google-ads/api/docs/client-libs/php/protobuf.md.txt) and the [Transport
guide](https://developers.google.com/google-ads/api/docs/client-libs/php/transport) for more information.

## Operation mode of the PHP interpreter

PHP is a versatile [scripting language](https://www.php.net/manual/en/introduction.php) and has several [operation
modes](https://www.php.net/manual/en/install.general.php) depending on usage. **PHP-FPM** (FastCGI Process Manager) or
persistent application servers have a notable advantage over standard CGI
because persistent worker processes can reuse cached bytecode (through OPcache)
and resources across requests.

## PHP version

Regularly upgrade to a newer PHP version to benefit from runtime performance
improvements. Check the [list of supported PHP versions](https://www.php.net/supported-versions.php).

## Unused Google Ads API versions

All versions of the client library support multiple Google Ads API versions.
For each version of the Google Ads API supported by the client library, there are
dedicated packages for that version.

The packages dedicated to versions of the Google Ads API that are not used by your
application can be safely removed from the client library. To reduce memory
footprint and speed up execution, the client library provides a utility to
remove unused API versions programmatically.

> [!WARNING]
> **Warning:** This removes files inside the `vendor` directory, which cannot easily be undone without reinstalling packages. Note that running `composer install` or `composer update` later will restore removed files unless you configure this script under `post-install-cmd` and `post-update-cmd` in your `composer.json`.

### Example

Suppose that you're implementing a client application that uses only the latest
API version (`v26`), and you want to remove support for the
unused API versions (`v23`,
`v24`, and `v25`).

> [!NOTE]
> **Note:** Each `google-ads-php` release bundles directories for the supported major API versions (such as `V26`, with minor releases like `v26.1` included under `V26`).

In your project's `composer.json` file, define a Composer script (named
`remove-google-ads-api-version-support`) that leverages the utility provided by
the client library in [`ApiVersionSupport`](https://github.com/googleads/google-ads-php/tree/HEAD/src/Google/Ads/GoogleAds/Util/ApiVersionSupport.php):

```json
"scripts": {
  "remove-google-ads-api-version-support": [
    "Google\\Ads\\GoogleAds\\Util\\ApiVersionSupport::remove"
  ]
}
```

Then, run the Composer script with the version numbers as parameters and print
status messages:

```bash
# Change the current directory to the project directory.
cd /path/to/the/project

# Install the project.
composer install

# Output the vendor folder size and the list of Google Ads API versions that are
# supported before removing support for unused Google Ads API versions.
echo "# Supported Google Ads API versions:"
find ./vendor/googleads/google-ads-php/src/Google/Ads/GoogleAds/V* \
  -maxdepth 0 | grep -oE 'V[0-9]+$'
echo "# Vendor folder size:"
du -sh ./vendor

# Use the Composer script to remove the unused versions
# v23, v24, and
# v25 of the Google Ads API.
echo "# Removing support..."
composer run-script remove-google-ads-api-version-support -- \
  23 \
  24 \
  25

# Output the vendor folder size and the list of Google Ads API versions that are
# supported after removing support for unused Google Ads API versions.
echo "# Supported Google Ads API versions:"
find ./vendor/googleads/google-ads-php/src/Google/Ads/GoogleAds/V* \
  -maxdepth 0 | grep -oE 'V[0-9]+$'
echo "# Vendor folder size:"
du -sh ./vendor
```

The following sample execution output shows the file size reduction, with
`V26` as the only remaining supported version:

    # Supported Google Ads API versions:
    V23
    V24
    V25
    V26
    # Vendor folder size:
    135M    ./vendor
    # Removing support...
    > Google\Ads\GoogleAds\Util\ApiVersionSupport::remove
    Removing support for the version 23 of Google Ads API...
    Done
    Removing support for the version 24 of Google Ads API...
    Done
    Removing support for the version 25 of Google Ads API...
    Done
    # Supported Google Ads API versions:
    V26
    # Vendor folder size:
    60M     ./vendor

> [!NOTE]
> **Note:** If you're developing on the client library project itself, a Composer script is already defined for your convenience: [`remove-api-version-support`](https://github.com/googleads/google-ads-php/blob/HEAD/composer.json).

## Development versus production

PHP is an [interpreted language](https://en.wikipedia.org/wiki/Interpreted_language) that compiles instructions
before executing them. This is advantageous during development, when source
files frequently change and execution time is less critical. In production
environments, stability and performance become the primary concerns.

### Cache

Caching is recommended because it improves performance and increases stability
by storing precompiled script bytecode in shared memory.

[OPcache](https://www.php.net/manual/en/book.opcache.php) is the standard solution and is bundled with PHP by
default.

### Autoload

[Autoloading](https://www.php.net/manual/en/language.oop5.autoload.php) improves performance and stability by loading
precompiled class map information.

The PHP client library conforms to [PSR-4](https://www.php-fig.org/psr/psr-4/) for autoloading and provides
the definition as part of its [`composer.json`](https://github.com/googleads/google-ads-php/blob/HEAD/composer.json) file. Dedicated
[Composer CLI options](https://getcomposer.org/doc/03-cli.md), such as `--optimize-autoloader` or
`--classmap-authoritative`, can then be used out of the box in production
builds.

### Logging

Setting loggers to a higher threshold such as `ERROR` or `WARNING` in production
helps reduce I/O overhead and memory consumption.

See the [Logging guide](https://developers.google.com/google-ads/api/docs/client-libs/php/logging.md.txt) for more information.

### Debugging and profiling

Disable debugger and profiler extensions (such as Xdebug) in production because
they introduce substantial execution overhead.

### Preload

Since PHP 7.4, [OPcache preloading](https://www.php.net/manual/en/opcache.configuration.php#ini.opcache.preload) can be used to preload
scripts into memory at server startup, going one step further than standard
OPcache caching.

A preload script must be designed specifically for your application. The PHP
client library does not provide a generic preload script because the trade-off
between memory usage and performance gain is specific to each project and
deployment environment.