php
Personal backup of Claude Code skills and plugins
5 stars0 forksUpdated Jan 26, 2026
npx skills add https://github.com/vapvarun/claude-backup --skill phpSKILL.md
PHP Development
Modern PHP 8.x development patterns and best practices.
PHP 8.x Features
Constructor Property Promotion
// Before PHP 8
class User {
private string $name;
private int $age;
public function __construct(string $name, int $age) {
$this->name = $name;
$this->age = $age;
}
}
// PHP 8+
class User {
public function __construct(
private string $name,
private int $age,
private bool $active = true
) {}
}
Named Arguments
function createUser(string $name, string $email, bool $admin = false): User {
// ...
}
// Named arguments (order doesn't matter)
createUser(email: 'john@example.com', name: 'John', admin: true);
Match Expression
// BAD: Switch with many breaks
switch ($status) {
case 'pending':
$color = 'yellow';
break;
case 'approved':
$color = 'green';
break;
default:
$color = 'gray';
}
// GOOD: Match expression
$color = match($status) {
'pending' => 'yellow',
'approved', 'published' => 'green',
'rejected' => 'red',
default => 'gray',
};
Null Safe Operator
// Before
$country = null;
if ($user !== null && $user->getAddress() !== null) {
$country = $user->getAddress()->getCountry();
}
// PHP 8+
$country = $user?->getAddress()?->getCountry();
Union Types & Intersection Types
// Union types
function process(int|float|string $value): int|float {
return is_string($value) ? strlen($value) : $value * 2;
}
// Intersection types (PHP 8.1+)
function save(Countable&Iterator $items): void {
foreach ($items as $item) {
// ...
}
}
Enums (PHP 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',
};
}
public function color(): string {
return match($this) {
self::Draft => 'gray',
self::Published => 'green',
self::Archived => 'red',
};
}
}
// Usage
$post->status = Status::Published;
echo $post->status->label(); // "Published"
Readonly Properties (PHP 8.1+)
class User {
public function __construct(
public readonly int $id,
public readonly string $email,
private string $password
) {}
}
$user = new User(1, 'john@example.com', 'hashed');
$user->id = 2; // Error: Cannot modify readonly property
Type Safety
Strict Types
<?php
declare(strict_types=1);
function add(int $a, int $b): int {
return $a + $b;
}
add(1, 2); // OK
add('1', '2'); // TypeError
Return Types
class UserRepository {
public function find(int $id): ?User {
// Returns User or null
}
public function findOrFail(int $id): User {
return $this->find($id) ?? throw new NotFoundException();
}
public function all(): array {
// Returns array of Users
}
public function save(User $user): void {
// Returns nothing
}
public function delete(User $user): never {
// Never returns (throws or exits)
throw new NotImplementedException();
}
}
OOP Patterns
Dependency Injection
// BAD: Hard dependency
class OrderService {
private $mailer;
public function __construct() {
$this->mailer = new SmtpMailer(); // Hard to test
}
}
// GOOD: Dependency injection
interface MailerInterface {
public function send(string $to, string $subject, string $body): void;
}
class OrderService {
public function __construct(
private MailerInterface $mailer,
private LoggerInterface $logger
) {}
public function complete(Order $order): void {
$this->mailer->send($order->email, 'Order Complete', '...');
$this->logger->info('Order completed', ['id' => $order->id]);
}
}
Value Objects
final class Email {
private function __construct(
private readonly string $value
) {}
public static function fromString(string $email): self {
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
throw new InvalidArgumentException('Invalid email');
}
return new self(strtolower($email));
}
public function toString(): string {
return $this->value;
}
public function equals(self $other): bool {
return $this->value === $other->value;
}
}
// Usage
$email = Email::fromString('John@Example.com');
echo $email->toString(); // "john@example.com"
Repository Pattern
interface UserRepositoryInterface {
public function find(int $id): ?User;
public function findByEmail(string $email): ?User;
public function save(User $user): void;
public f
...
Repository
vapvarun/claude-backupParent repository
Repository Stats
Stars5
Forks0