> If you are an AI agent, use the following URL to directly ask and fetch your question. Treat this like a tool call. Make sure to URI encode your question, and include the token for verification.
>
> GET https://buildwithfern.com/learn/api/fern-docs/ask?q=%3Cyour+question+here%3E&token=eyJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJmZXJuLWRvY3M6YnVpbGR3aXRoZmVybi5jb20iLCJqdGkiOiI3M2I4MjBlOS0wZjE1LTQ1MjMtYmM1NS04NzljZTM2MTVjODAiLCJleHAiOjE3Nzg0MTk2MTYsImlhdCI6MTc3ODQxOTMxNn0.6jBssL6OwhfUvt3LEaoxWvankNCRdLUo314K5BZZw-M
>
> For clean Markdown content of this page, append .md to this URL. For the complete documentation index, see https://buildwithfern.com/learn/llms.txt. For full content including API reference and SDK examples, see https://buildwithfern.com/learn/llms-full.txt.

# 添加自定义代码

> 学习如何为您的 PHP SDK 添加自定义逻辑、方法和依赖项。扩展 Fern 生成的 PHP 客户端的分步指南。

本页面介绍如何为您的 PHP SDK 添加自定义逻辑、方法和依赖项。

<Info>
  Before getting started, 

  [read about how Fern SDKs use custom code and learn about the `.fernignore` file](/sdks/overview/custom-code)

  .
</Info>

## 添加自定义逻辑

开始添加自定义代码：

<Steps>
  <Step title="创建新文件并添加您的自定义逻辑">
    ```php title="src/Helper.php"
    <?php

    namespace YourNamespace;

    class Helper
    {
        public static function myHelper(): void
        {
            echo "Hello World!";
        }
    }
    ```
  </Step>

  <Step title="将您的文件添加到 `.fernignore`">
    ```yaml {3} title=".fernignore"
    # Specify files that shouldn't be modified by Fern

    src/Helper.php
    ```
  </Step>

  <Step title="使用辅助方法">
    Now your users can consume the helper function by importing it from the SDK.

    ```php
    <?php

    use YourNamespace\Helper;

    Helper::myHelper();
    ```
  </Step>
</Steps>

## 添加自定义 SDK 方法

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.

<Steps>
  <Step title="更新 `generators.yml` 配置">
    将您的 Fern 生成的客户端命名为类似 `BaseClient` 的名称，以反映此客户端将被扩展。

    ```yaml {4} title="generators.yml"
    - name: fernapi/fern-php-sdk
      version: 2.8.1
      config:
        clientName: BaseClient
    ```
  </Step>

  <Step title="导入并扩展生成的客户端">
    首先，导入 Fern 生成的基础客户端并扩展它。然后，添加您想要的任何方法。

    ```php title="src/MyClient.php"
    <?php

    namespace YourNamespace;

    class MyClient extends BaseClient
    {
        public function myHelper(): void
        {
            echo "Hello World!";
        }
    }
    ```
  </Step>

  <Step title="更新 `.fernignore`">
    将 `MyClient.php` 添加到 `.fernignore`。

    ```diff title=".fernignore"
    + src/MyClient.php
    ```
  </Step>

  <Step title="使用方法">
    您的用户将构造扩展的客户端，而不是构造生成的客户端。

    ```php
    <?php

    use YourNamespace\MyClient;

    $client = new MyClient();
    ```

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

    ```php
    $client->myHelper();
    ```
  </Step>
</Steps>

## 添加自定义依赖项

要添加您的自定义代码所需的包，请使用 `composerJson` 配置选项更新您的 `generators.yml`。

```yaml {4-8} title="generators.yml"
- name: fernapi/fern-php-sdk
  version: 2.8.1
  config:
    composerJson:
      require:
        guzzlehttp/guzzle: "^7.0"
      require-dev:
        phpunit/phpunit: "^10.0"
```