Command Palette

Search for a command to run...

UnylyUnyly
Назад к скиллам

PHP Modern Features

БесплатноБез исполняемых скриптовНе проверен

Use when modern PHP features including typed properties, union types, match expressions, named arguments, attributes, enums, and patterns for writing…

TheBushidoCollectiveот TheBushidoCollective★ 202 звезды на GitHub

Об этом скилле

PHP Modern Features

Introduction

Modern PHP (7.4+, 8.0+, 8.1+, 8.2+) has evolved dramatically with features that improve type safety, expressiveness, and developer experience. These additions transform PHP from a loosely-typed scripting language into a powerful, type-safe platform for building robust applications.

Key improvements include strict typing, property and parameter types, union and intersection types, match expressions, named arguments, attributes (annotations), enumerations, and readonly properties. These features enable clearer intent, better IDE support, and fewer runtime errors.

This skill covers typed properties, union/intersection types, match expressions, enums, attributes, named arguments, and patterns for leveraging modern PHP effectively.

Typed Properties and Parameters

Type declarations improve code reliability by enforcing types at runtime and enabling better static analysis.

<?php
// PHP 7.4+ typed properties
class User {
    public string $name;
    public int $age;
    public ?string $email = null; // Nullable
    private array $roles = [];
    protected bool $active = true;
}

$user = new User();
$user->name = "Alice"; // OK
$user->age = 30; // OK
// $user->age = "thirty"; // TypeError

// Constructor property promotion (PHP 8.0+)
class Product {
    public function __construct(
        public string $name,
        public float $price,
        public int $stock,
        private ?string $sku = null
    ) {}
}

$product = new Product("Laptop", 999.99, 10);
echo $product->name; // "Laptop"
// echo $product->sku; // Error: private property

// Return type declarations
function calculateTotal(float $price, int $quantity): float {
    return $price * $quantity;
}

function findUser(int $id): ?User {
    // Return User or null
    return $id > 0 ? new User() : null;
}

function getUsers(): array {
    return [new User(), new User()];
}

// Void return type
function logMessage(string $message): void {
    echo $message . "\n";
}

// Never return type (PHP 8.1+)
function fail(string $message): never {
    throw new Exception($message);
}

// Mixed type (PHP 8.0+)
function process(mixed $value): mixed {
    return $value;
}

// Static return type
class Builder {
    public function setName(string $name): static {
        return $this;
    }

    public function build(): static {
        return new static();
    }
}

// Parameter types
function greet(
    string $name,
    int $age,
    bool $formal = false
): string {
    $greeting = $formal ? "Good day" : "Hello";
    return "$greeting, $name ($age)";
}

// Variadic typed parameters
function sum(int ...$numbers): int {
    return array_sum($numbers);
}

$total = sum(1, 2, 3, 4, 5); // 15

Typed properties and parameters catch type errors early and provide clear contracts for function interfaces.

Union and Intersection Types

Union types allow multiple type possibilities, while intersection types require all specified types simultaneously.

<?php
// Union types (PHP 8.0+)
function processId(int|string $id): void {
    if (is_int($id)) {
        echo "Processing integer ID: $id\n";
    } else {
        echo "Processing string ID: $id\n";
    }
}

processId(123);
processId("ABC-456");

// Union type properties
class Response {
    public function __construct(
        public int|string $code,
        public array|string $data
    ) {}
}

// Nullable as union type
function findProduct(int $id): Product|null {
    return $id > 0 ? new Product("Item", 10.0, 5) : null;
}

// Multiple union types
function format(int|float|string $value): string {
    return match (true) {
        is_int($value) => "Integer: $value",
        is_float($value) => "Float: $value",
        is_string($value) => "String: $value",
    };
}

// False pseudo-type in unions
function parseValue(string $input): int|false {
    $result = filter_var($input, FILTER_VALIDATE_INT);
    return $result !== false ? $result : false;
}

// Union types with built-in types
function getData(): array|object|null {
    return ['key' => 'value'];
}

// Intersection types (PHP 8.1+)
interface Loggable {
    public function log(): void;
}

interface Cacheable {
    public function cache(): void;
}

// Function accepting intersection type
function process(Loggable&Cacheable $object): void {
    $object->log();
    $object->cache();
}

class Service implements Loggable, Cacheable {
    public function log(): void {
        echo "Logging\n";
    }

    public function cache(): void {
        echo "Caching\n";
    }
}

// Intersection with union
function handle((Loggable&Cacheable)|null $object): void {
    $object?->log();
}

// DNF types (PHP 8.2+ - Disjunctive Normal Form)
function advanced((Loggable&Cacheable)|(Loggable&Serializable) $object): void {
    $object->log();
}

Union types enable flexible parameter acceptance while intersection types enforce multiple capabilities simultaneously.

Match Expressions

Match expressions provide pattern matching

Полный SKILL.md на GitHub

Установить PHP Modern Features в Claude Code и Claude Desktop

Зарегайся, чтобы установить скилл

Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.

  • Открой команду установки в одну строку
  • Сохраняй скиллы в синхронизируемую библиотеку
  • Уведомления, когда скиллы обновляются
Зарегаться бесплатноУ меня уже есть аккаунт

Разрешённые инструменты

Инструменты, которые скиллу разрешено вызывать.

Без ограничений — скилл может использовать любой инструмент.

FAQ

Что делает скилл PHP Modern Features?

Use when modern PHP features including typed properties, union types, match expressions, named arguments, attributes, enums, and patterns for writing type-safe, expressive PHP code with latest language improvements.

Как установить скилл PHP Modern Features?

Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.

Скилл PHP Modern Features запускает скрипты?

Нет, скилл состоит только из инструкций (SKILL.md), без исполняемых скриптов.

Похожие скиллы

Сравнить PHP Modern Features с