php-idioms
Skill157 starsChanged 3 months agoMITView on GitHub
Modern PHP 8.x idioms: strict types, typed properties, enums, readonly classes, match expressions, and PSR standards. Use when writing, refactoring, or reviewing pure PHP code, domain services, or libraries. Pair with laravel-idioms for Laravel applications.
In this repository59 other files it gives its agents.
- github-pages/agents.mdAGENTS.md
- adrSkill
- agent-protocolsSkill
- angular-idiomsSkill
- api-documentationSkill
- axum-idiomsSkill
- browser-automationSkill
- chaos-testingSkill
- ci-cdSkill
- cli-developmentSkill
- code-auditSkill
- code-reviewSkill
- cpp-idiomsSkill
- csharp-idiomsSkill
- data-engineeringSkill
- debugging-protocolSkill
- django-idiomsSkill
- dotnet-idiomsSkill
- elixir-idiomsSkill
- embedded-systemsSkill
- feature-flagsSkill
- flutter-idiomsSkill
- frontend-designSkill
- git-commit-integritySkill
- go-idiomsSkill
- guardrailsSkill
- hono-idiomsSkill
- incident-responseSkill
- java-idiomsSkill
- javascript-idiomsSkill
- kotlin-idiomsSkill
- laravel-idiomsSkill
- logging-implementationSkill
- ml-engineeringSkill
- mobile-designSkill
- mobile-testingSkill
- nextjs-idiomsSkill
- parallel-dispatchSkill
- payment-integrationSkill
- perf-optimizationSkill
- postgres-idiomsSkill
- python-idiomsSkill
- rails-idiomsSkill
- react-idiomsSkill
- refactoring-patternsSkill
- research-methodologySkill
- ruby-idiomsSkill
- rust-idiomsSkill
- security-auditSkill
- sequential-thinkingSkill
- spring-boot-idiomsSkill
- sql-idiomsSkill
- structured-specSkill
- supply-chain-securitySkill
- swift-idiomsSkill
- testability-patternsSkill
- testing-strategySkill
- typescript-idiomsSkill
- vue-idiomsSkill
The file
Files
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:
enum Priority: string { case Low = 'low'; case Medium = 'medium'; case High = 'high'; } - Readonly classes for immutable DTOs:
readonly class CreateTaskRequest { public function __construct( public string $title, public Priority $priority, ) {} } - Named arguments for clarity:
$task = new Task(title: 'Deploy fix', priority: Priority::High); - Match expressions over switch:
$score = match($priority) { Priority::Low => 1, Priority::Medium => 5, Priority::High => 10, };
Error Handling
- Domain exception hierarchies:
abstract class DomainException extends \RuntimeException {} class NotFoundException extends DomainException { public function __construct( public readonly string $resource, public readonly string $resourceId, ) { parent::__construct("{$resource} '{$resourceId}' not found"); } } - Never
catch (\Exception)without re-throw or specific handling. - Type-safe return types — never mixed without justification.
Interfaces and DI
- Define interface where consumed, implement where provided:
// In task feature interface TaskStorage { public function getById(string $id): Task; public function save(Task $task): void; } // Constructor injection class TaskService { public function __construct( private readonly TaskStorage $storage, ) {} }
Naming (PSR-12)
- PascalCase for classes, interfaces, traits, enums.
- camelCase for methods, properties.
- UPPER_SNAKE_CASE for constants.
- PSR-4 autoloading — namespace = directory path.
Testing
- PHPUnit or Pest:
test('calculate discount returns zero for no items', function () { $result = $this->calculator->calculateDiscount([], $this->coupon); expect($result)->toBe(0.0); }); - Data providers for parameterized tests.
- Mockery or PHPUnit mocks — never test implementation details.
Formatting and Static Analysis
| Tool | Purpose | Command |
| PHP CS Fixer | PSR-12 formatting | php-cs-fixer fix . |
| PHPStan (level 9) | Static analysis | phpstan analyse src/ --level 9 |
| Psalm | Type checking | psalm --show-info=true |
composer audit | CVE scanning | composer audit |
Related
- Code Idioms and Conventions .agents/rules/code-idioms-and-conventions.md
- Testing Strategy .agents/rules/testing-strategy.md
- Error Handling Principles .agents/rules/error-handling-principles.md
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

