Skip to main content

Introducing PHP 8.1 Compatibility in SDKs

· 2 min read

The APIMatic Code Generator now supports SDK compatibility with the runtime version of PHP 8.1.

Previously, the PHP SDK was compatible with PHP versions 7.2. to 8.0., and the end-users using PHP 8.1 were not able to install or use the SDKs within their PHP 8.1 applications. Now with the introduction of PHP 8.1 support in APIMatic's Code Generation engine, PHP 8.1 users will not only be able to install the SDKs, but also enjoy a seamless bug-free experience.

What's Changed?

PHP 8.1 runtime takes mismatching tentative return types very serious, hence users are served with a deprecation notice whenever the actual return type of an inherited function differs with the base function's return type. This deprecation notice causes runtime failure during inheritance of JsonSerializable in the SDKs, as changing the return type of jsonSerialize(), which can be seen in the following code snippet:

class Model implements JsonSerializable {
public function jsonSerialize()
{
$json = [];
$json['session'] = $this->session;

return $json;
}
}

Here, jsonSerialize() is inherited from JsonSerializable and its return type was mixed but now changed to array. With the introduction of PHP 8.1 this will be result in a runtime exception during the inheritance of JsonSerializable. So to avoid this failure, PHP 8.1 suggested approach has been followed where the attribute #[\ReturnTypeWillChange] for such cases is used. Following all this, the above code snippet looks like this:

class Model implements JsonSerializable {
#[\ReturnTypeWillChange]
public function jsonSerialize()
{
$json = [];
$json['session'] = $this->session;

return $json;
}
}

For more detail on the features that SDKs support, please refer to our documentation.