Php pro

From findscripter/everything-skills · View interactive version

Php pro is an AI agent skill from the GitHub repository findscripter/everything-skills. 当用 PHP 8+ 写高性能后端、处理大数据集或重构遗留 OOP 代码时使用;产出用生成器/迭代器/SPL 节省内存、强类型且 PSR 合规、含完善异常处理的惯用实现;不适用于其他语言、仅需基础 PHP 语法解释、或无法改动运行时与依赖配置的场景;触发词:生成器、SPL、PHP 8 枚举、严格类型、性能剖析

How to use this skill

Skill file

何时使用

适用:

不该用(负边界):

步骤

  1. 对齐前提:确认 PHP 版本(8.0/8.1/8.2+)、运行时、扩展(OPcache)与部署约束。
  2. 优先内建:先用 PHP 标准库与内建函数,再考虑自己实现或引第三方包。
  3. 选型:依据数据规模与访问模式选生成器 vs SPL 结构(SplQueue/SplStack/SplHeap/ArrayObject)。
  4. 实现:开 declare(strict_types=1),全量类型标注,异常处理显式,命名自解释。
  5. 优化:先用 Xdebug/Blackfire 剖析定位热点,再针对性优化——不要凭直觉调。

指令

示例

用生成器流式读大文件,内存占用与文件大小解耦:

<?php
declare(strict_types=1);

/** @return \Generator<int, string> */
function readLines(string $path): \Generator
{
    $fh = fopen($path, 'rb');
    if ($fh === false) {
        throw new \RuntimeException("无法打开文件: {$path}");
    }
    try {
        while (($line = fgets($fh)) !== false) {
            yield rtrim($line, "\r\n");   // 一次只驻留一行
        }
    } finally {
        fclose($fh);
    }
}

foreach (readLines('huge.log') as $line) {
    // 逐行处理,内存恒定
}

PHP 8 枚举 + match,替代魔法常量与 if-else 链:

<?php
declare(strict_types=1);

enum Status: string
{
    case Active   = 'active';
    case Disabled = 'disabled';

    public function label(): string
    {
        return match ($this) {
            self::Active   => '已启用',
            self::Disabled => '已停用',
        };
    }
}

典型请求:

注意事项

互见


采编自 sickn33/antigravity-awesome-skills(MIT)。

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.