Php Idioms
This skill covers idiomatic PHP 8.x development, emphasizing strict typing, immutable data structures via readonly classes and enums, and domain-driven error handling. Learn named arguments, match expressions, constructor injection, and PSR-12 naming conventions alongside static analysis tools like PHPStan and Psalm.
PHP Idioms teaches modern PHP 8.x patterns for type-safe, immutable, and framework-agnostic code.
AI-generated summary based on this skill's SKILL.md
Install
irahardianto/awesome-agv/php-idioms · repository language: JavaScript
git clone https://github.com/irahardianto/awesome-agv
cp -r awesome-agv/.agents/skills/php-idioms ~/.claude/skills/php-idiomsnpx skillfed install irahardianto/awesome-agv/php-idiomsFrequently asked questions
AI-generated answers based on this skill's SKILL.md and metadata
What modern PHP 8.x idioms and best practices should I follow?
Php Idioms teaches strict typing with declare(strict_types=1), named arguments for clarity, match expressions over switch statements, and constructor injection for dependency management. Use readonly classes and enums for immutable data, leverage union and intersection types, and adopt PSR-12 naming conventions. These patterns make code type-safe, testable, and maintainable across PHP 8.x projects.
How do I implement PSR-12 compliant naming and structure?
Php Idioms covers PSR-12 standards for class names (PascalCase), method names (camelCase), constants (UPPER_SNAKE_CASE), and file organization. Apply these conventions consistently across namespaces, use PHP CS Fixer to automate formatting, and configure your IDE to enforce rules. This ensures your codebase remains readable and aligns with community standards.
What static analysis and formatting tools should I set up?
Php Idioms recommends PHPStan and Psalm for static type checking, and PHP CS Fixer for code formatting. Configure these tools in your CI/CD pipeline to catch type errors, unused variables, and style violations before deployment. Combined with PHPUnit or Pest for testing, they form a robust quality gate for PHP projects.
How can I design immutable DTOs and domain exceptions in PHP?
Php Idioms teaches readonly classes and enums to enforce immutability in Data Transfer Objects. Design domain exceptions as a hierarchy with specific exception classes for different error scenarios. Use constructor promotion and strict typing to ensure DTOs are type-safe and exceptions carry meaningful context for error handling.
What are PHP match expressions and how do they differ from switch?
Php Idioms explains that PHP 8.x match expressions are stricter than switch statements—they require exhaustive cases, return values directly, and use strict (===) comparison by default. Match is ideal for pattern matching and cleaner syntax when mapping inputs to outputs, reducing boilerplate and potential bugs.
How do I write testable PHP with proper dependency injection?
Php Idioms covers constructor injection as the primary pattern: accept dependencies as constructor parameters typed with interfaces, not concrete classes. This decouples your code, enables easy mocking in tests, and follows SOLID principles. Combine with PHPUnit or Pest frameworks to verify behavior without tight coupling.
SKILL.md
rendered from the published skill — quoted content, verbatim
PHP Idioms and Patterns
Modern PHP (8.x) rewards type safety, immutability, and framework-agnostic design. Lean into typed properties, enums, and readonly classes. Idiomatic PHP = strict types, PSR-compliant, well-tested.
> Scope: PHP coding idioms. Test naming: .agents/rules/testing-strategy.md. Logging: @.agents/skills/logging-implementation/SKILL.md.
Modern PHP Features (8.x)
-
declare(strict_types=1)— always, every file. -
Enums for domain constants:
php enum Priority: string { case Low = 'low'; case Medium = 'medium'; case High = 'high'; } -
Readonly classes for immutable DTOs:
php readonly class CreateTaskRequest { public function __construct( public string $title, public Priority $priority, ) {} } -
Named arguments for clarity:
php $task = new Task(title: 'Deploy fix', priority: Priority::High); -
Match expressions over switch:
php $score = match($priority) { Priority::Low => 1, Priority::Medium => 5, Priority::High => 10, };
Error Handling
1.
(truncated - see the full file via the links below)
Read as markdown · JSON record · Browse the source repository
File tree — 1 file
.agents/skills/php-idioms/SKILL.md