Php

From G1Joshi/Agent-Skills · View interactive version

Php is an AI agent skill from the GitHub repository G1Joshi/Agent-Skills. Expert modern PHP (PHP 8.2/8.3+) assistance covering strict typing, attributes, fibers, Composer, and OPcache. Use when building Laravel/Symfony web apps, developing REST APIs, or optimizing PHP workloads.

How to use this skill

Skill file

PHP

A popular specific-purpose scripting language that is especially suited to web development.

When to Use

Quick Start

<?php
$name = "World";
echo "Hello, $name!";

$colors = ["red", "green", "blue"];
foreach ($colors as $color) {
    echo $color . "<br>";
}
?>

Core Concepts

Modern Strict Typing & Constructor Promotion (PHP 8.2+)

Enforces strict scalar types, readonly classes, and promoted constructor properties:

<?php
declare(strict_types=1);

namespace App\Domain;

final readonly class Order
{
    public function __construct(
        public string $id,
        public float $amount,
        public OrderStatus $status = OrderStatus::Pending,
        public \DateTimeImmutable $createdAt = new \DateTimeImmutable()
    ) {}
}

enum OrderStatus: string
{
    case Pending = 'PENDING';
    case Paid = 'PAID';
    case Shipped = 'SHIPPED';
}

Expressive `match` Expressions & First-Class Callables

Replaces verbose switch statements with strict type comparison and returned values:

function getDiscountPercentage(OrderStatus $status): float
{
    return match ($status) {
        OrderStatus::Pending => 0.0,
        OrderStatus::Paid    => 0.05,
        OrderStatus::Shipped => 0.10,
    };
}

Modern Fiber-Based Concurrency & Persistent Runtimes

Runs concurrent fibers and persistent memory workers with FrankenPHP:

$fiber = new Fiber(function (): void {
    $value = Fiber::suspend('paused');
    echo "Resumed with: $value
";
});

$result = $fiber->start();
$fiber->resume('active');

Common Patterns

Constructor Promotion and Readonly Properties

Problem: Verbose boilerplate required to declare, type, and assign class properties.

Solution:

Use PHP 8 constructor property promotion with readonly:

<?php
declare(strict_types=1);

readonly class UserProfile
{
    public function __construct(
        public int $id,
        public string $name,
        public string $email,
        public array $roles = ['user']
    ) {}
}

$user = new UserProfile(1, 'Jane', 'jane@example.com');
echo $user->name;

Best Practices

Do:

Don't:

Troubleshooting

| Error | Cause | Solution |

| :-------------------------------------------------------- | :------------------------------------------------------ | :-------------------------------------------------------------------------------- |

| TypeError: Argument 1 passed must be of the type ... | Strict types enabled and caller passed mismatched type. | Cast argument explicitly or verify caller inputs. |

| Fatal error: Allowed memory size of ... bytes exhausted | Script exceeded memory_limit loading large dataset. | Use generators (yield) or increase memory_limit in php.ini. |

| Class '...' not found | Autoloader not included or Composer autoloader stale. | Add require __DIR__ . '/vendor/autoload.php'; and run composer dump-autoload. |

References

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.