developing-with-php
Modern PHP 8.x development with type system, attributes, enums, error handling, and Composer. Use when writing PHP code or working with PHP projects.
What this skill does
# PHP Skill - Quick Reference
Core PHP language patterns for modern development. For Laravel-specific patterns, see the Laravel skill.
> **Progressive Disclosure**: This is the quick reference. See [REFERENCE.md](./REFERENCE.md) for comprehensive patterns, advanced examples, and deep-dives.
## Table of Contents
1. [PHP 8.x Type System](#php-8x-type-system)
2. [Constructor Property Promotion](#constructor-property-promotion)
3. [Enums](#enums)
4. [Match Expression & Named Arguments](#match-expression--named-arguments)
5. [Attributes](#attributes)
6. [Null Safety](#null-safety)
7. [OOP Essentials](#oop-essentials)
8. [Handler/Service Pattern](#handlerservice-pattern)
9. [Map/DTO Pattern](#mapdto-pattern)
10. [PDO Database Access](#pdo-database-access)
11. [Composer & Autoloading](#composer--autoloading)
12. [Error Handling](#error-handling)
13. [Testing with PHPUnit](#testing-with-phpunit)
14. [Array Operations Quick Reference](#array-operations-quick-reference)
15. [Security Essentials](#security-essentials)
16. [Quick Reference Tables](#quick-reference-tables)
17. [Related Resources](#related-resources)
---
## PHP 8.x Type System
```php
<?php
// Union types (8.0+)
function process(string|int $value): string|false { }
// Intersection types (8.1+)
function handle(Countable&Iterator $collection): void { }
// Nullable types
function find(int $id): ?User { }
// Never return type (8.1+)
function fail(): never {
throw new Exception('Fatal error');
}
// True, false, null as standalone types (8.2+)
function isValid(): true { return true; }
```
## Constructor Property Promotion
```php
<?php
// Promoted properties (8.0+) - replaces boilerplate
class User {
public function __construct(
private string $name,
private string $email,
private bool $active = true,
) {}
}
// Readonly class (8.2+) - all properties are readonly
readonly class ValueObject {
public function __construct(
public string $value,
public DateTimeImmutable $createdAt,
) {}
}
```
## Enums
```php
<?php
// Backed enum with values (8.1+)
enum Status: string {
case Draft = 'draft';
case Published = 'published';
case Archived = 'archived';
public function label(): string {
return match($this) {
self::Draft => 'Draft',
self::Published => 'Published',
self::Archived => 'Archived',
};
}
}
// Usage
$status = Status::Published;
$value = $status->value; // 'published'
$all = Status::cases(); // Array of all cases
$fromValue = Status::from('draft'); // Status::Draft
$tryFrom = Status::tryFrom('invalid'); // null (no exception)
```
> See [REFERENCE.md](./REFERENCE.md#enum-patterns) for EnumTrait pattern and advanced enum usage.
## Match Expression & Named Arguments
```php
<?php
// Match returns value, uses strict comparison
$result = match($status) {
Status::Draft => 'Not ready',
Status::Published => 'Live',
Status::Archived => 'Hidden',
};
// Multiple conditions
$category = match($code) {
200, 201, 204 => 'success',
400, 422 => 'client_error',
500, 502, 503 => 'server_error',
default => 'unknown',
};
// Named arguments (skip defaults, any order)
$user = createUser(
email: '[email protected]',
name: 'John Doe',
role: 'admin',
);
```
## Attributes
```php
<?php
#[Attribute]
class Route {
public function __construct(
public string $path,
public string $method = 'GET',
) {}
}
class UserController {
#[Route('/users', 'GET')]
public function index(): array { }
}
// Reading attributes via reflection
$reflection = new ReflectionMethod(UserController::class, 'index');
$attributes = $reflection->getAttributes(Route::class);
$route = $attributes[0]->newInstance();
echo $route->path; // '/users'
```
## Null Safety
```php
<?php
// Null coalescing
$name = $user->name ?? 'Anonymous';
// Null coalescing assignment
$data['count'] ??= 0;
// Nullsafe operator (8.0+)
$country = $user?->address?->country?->name;
// Combined
$timezone = $user?->settings?->timezone ?? 'UTC';
```
---
## OOP Essentials
### Interfaces & Abstract Classes
```php
<?php
interface RepositoryInterface {
public function find(int $id): ?object;
public function save(object $entity): void;
}
abstract class BaseHandler {
public function __construct(protected PDO $db) {}
abstract public function handle(array $data): mixed;
}
```
### Traits
```php
<?php
trait Timestamps {
protected ?DateTimeImmutable $createdAt = null;
public function setCreatedAt(): void {
$this->createdAt = new DateTimeImmutable();
}
}
class User {
use Timestamps;
}
```
> See [REFERENCE.md](./REFERENCE.md#oop-patterns) for trait conflict resolution, late static binding, and magic methods.
---
## Handler/Service Pattern
```php
<?php
class UserHandler {
public function __construct(
private readonly PDO $db,
private readonly CacheInterface $cache,
) {}
public function get(int $id): ?array {
$cacheKey = "user:{$id}";
if ($cached = $this->cache->get($cacheKey)) {
return $cached;
}
$stmt = $this->db->prepare('SELECT * FROM users WHERE id = ?');
$stmt->execute([$id]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);
if ($user) {
$this->cache->set($cacheKey, $user, 3600);
}
return $user ?: null;
}
}
```
> See [REFERENCE.md](./REFERENCE.md#handler-patterns) for CRUD handlers, aggregation patterns, and collection mapping.
---
## Map/DTO Pattern
```php
<?php
readonly class UserMap {
public function __construct(
public int $id,
public string $name,
public string $email,
) {}
public static function fromRow(array $row): self {
return new self(
id: (int) $row['id'],
name: trim($row['first_name'] . ' ' . $row['last_name']),
email: $row['email'],
);
}
public function toArray(): array {
return ['id' => $this->id, 'name' => $this->name, 'email' => $this->email];
}
}
```
> See [REFERENCE.md](./REFERENCE.md#dto-patterns) for nested mapping and collection patterns.
---
## PDO Database Access
### Connection
```php
<?php
$dsn = 'mysql:host=localhost;dbname=myapp;charset=utf8mb4';
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
$pdo = new PDO($dsn, 'username', 'password', $options);
```
### Prepared Statements
```php
<?php
// Named parameters
$stmt = $pdo->prepare('SELECT * FROM users WHERE email = :email');
$stmt->execute(['email' => $email]);
$user = $stmt->fetch();
// Positional parameters
$stmt = $pdo->prepare('SELECT * FROM users WHERE id = ?');
$stmt->execute([$id]);
```
### Transactions
```php
<?php
try {
$pdo->beginTransaction();
// Multiple operations...
$pdo->commit();
} catch (Exception $e) {
$pdo->rollBack();
throw $e;
}
```
> See [REFERENCE.md](./REFERENCE.md#pdo-advanced) for stored procedures, batch operations, and SQL file execution.
---
## Composer & Autoloading
### composer.json Essentials
```json
{
"require": {
"php": "^8.2"
},
"autoload": {
"psr-4": {
"App\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
}
}
```
### Version Constraints
| Constraint | Meaning |
|------------|---------|
| `^1.0` | `>=1.0.0 <2.0.0` |
| `~1.2` | `>=1.2.0 <1.3.0` |
| `1.0.*` | `>=1.0.0 <1.1.0` |
---
## Error Handling
### Exception Pattern
```php
<?php
class AppException extends Exception {
public function __construct(
string $message,
int $code = 0,
public readonly array $context = [],
) {
parent::__construct($message, $code);
}
}
class NotFoundException extends AppExRelated in Writing & Docs
jax-development
IncludedUse this skill when the user is writing, debugging, profiling, refactoring, reviewing, benchmarking, parallelising, exporting, or explaining JAX code, or when they mention JAX, jax.numpy, jit, grad, value_and_grad, vmap, scan, lax, random keys, pytrees, jax.Array, sharding, Mesh, PartitionSpec, NamedSharding, pmap, shard_map, Pallas, XLA, StableHLO, checkify, profiler, or the JAX repo. It helps turn NumPy or PyTorch-style code into pure functional JAX, fix tracer/control-flow/shape/PRNG bugs, remove recompiles and host-device syncs, choose transforms and sharding strategies, inspect jaxpr/lowering/IR, and benchmark compiled code correctly.
nature-article-writer
IncludedDrafts, rewrites, diagnostically critiques, and style-calibrates primary research manuscripts for Nature and Nature Portfolio journals. Use when the user wants a Nature-style title, summary paragraph or abstract, introduction, results, discussion, methods, figure legends, presubmission enquiry, cover letter, reviewer response, or when a scientific draft sounds generic, jargon-heavy, structurally weak, or AI-ish and needs precise, broad-reader-friendly prose without inventing data, analyses, or references. Best for primary research articles and letters rather than reviews or press releases unless explicitly adapting one.
deckrd
IncludedDocument-driven framework that derives requirements, specifications, implementation plans, and executable tasks from goals through structured AI dialogue. Use when user says "write requirements", "create spec", "plan implementation", "derive tasks", "structure this feature", "break down into tasks", or "document this module". Also use for reverse engineering existing code into docs (/deckrd rev). Do NOT use for direct code writing — use /deckrd-coder after tasks are generated. Do NOT use when the user only wants to run or fix existing code without planning.
clinical-decision-support
IncludedGenerate professional clinical decision support (CDS) documents for pharmaceutical and clinical research settings, including patient cohort analyses (biomarker-stratified with outcomes) and treatment recommendation reports (evidence-based guidelines with decision algorithms). Supports GRADE evidence grading, statistical analysis (hazard ratios, survival curves, waterfall plots), biomarker integration, and regulatory compliance. Outputs publication-ready LaTeX/PDF format optimized for drug development, clinical research, and evidence synthesis.
handling-sf-data
IncludedSalesforce data operations with 130-point scoring. Use this skill to create, update, delete, bulk import/export, generate test data, and clean up org records using sf CLI and anonymous Apex. TRIGGER when: user creates test data, performs bulk import/export, uses sf data CLI commands, needs data factory patterns for Apex tests, or needs to seed/clean records in a Salesforce org. DO NOT TRIGGER when: SOQL query writing only (use querying-soql), Apex test execution (use running-apex-tests), or metadata deployment (use deploying-metadata).
accelint-ac-to-playwright
IncludedConvert and validate acceptance criteria for Playwright test automation. Use when user asks to (1) review/evaluate/check if AC are ready for automation, (2) assess if AC can be converted as-is, (3) validate AC quality for Playwright, (4) turn AC into tests, (5) generate tests from acceptance criteria, (6) convert .md bullets or .feature Gherkin files to Playwright specs, (7) create test automation from requirements. Handles both bullet-style markdown and Gherkin syntax with JSON test plan generation and validation.