Adding custom code

View as Markdown

This page covers how to add custom logic, methods, and dependencies to your PHP SDK.

Adding custom logic

To get started adding custom code:

1

Create a new file and add your custom logic

src/Helper.php
1<?php
2
3namespace YourNamespace;
4
5class Helper
6{
7 public static function myHelper(): void
8 {
9 echo "Hello World!";
10 }
11}
2

Add your file to .fernignore

.fernignore
1# Specify files that shouldn't be modified by Fern
2
3src/Helper.php
3

Consume the helper

Now your users can consume the helper function by importing it from the SDK.

1<?php
2
3use YourNamespace\Helper;
4
5Helper::myHelper();

Adding custom SDK methods

Fern also allows you to add custom methods to the SDK itself (e.g. client.my_method() ) by inheriting the Fern generated client and then extending it.

1

Update generators.yml configuration

Name your Fern-generated client something like BaseClient to reflect that this client will be extended.

generators.yml
1- name: fernapi/fern-php-sdk
2 version: 1.25.1
3 config:
4 clientName: BaseClient
2

Import and extend the generated client

First, import the Fern generated base client and extend it. Then, add whatever methods you want.

src/MyClient.php
1<?php
2
3namespace YourNamespace;
4
5class MyClient extends BaseClient
6{
7 public function myHelper(): void
8 {
9 echo "Hello World!";
10 }
11}
3

Update .fernignore

Add the MyClient.php to .fernignore.

.fernignore
1+ src/MyClient.php
4

Consume the method

Instead of constructing the generated client, your users will want to construct the extended client.

1<?php
2
3use YourNamespace\MyClient;
4
5$client = new MyClient();

Now your users can consume the helper function by importing it from the SDK.

1$client->myHelper();

Adding custom dependencies

To add packages that your custom code requires, update your generators.yml using the composerJson configuration option.

generators.yml
1- name: fernapi/fern-php-sdk
2 version: 1.25.1
3 config:
4 composerJson:
5 require:
6 guzzlehttp/guzzle: "^7.0"
7 require-dev:
8 phpunit/phpunit: "^10.0"