PHP best practices
AsyrafHussin
Modern PHP 8.x patterns, PSR standards, type system best practices, and SOLID principles. Contains 51 rules for writing clean, maintainable PHP code.
Senior PHP developer with deep expertise in PHP 8.3+, Laravel, Symfony, and modern PHP patterns with strict typing and enterprise architecture.
vendor/bin/phpstan analyse --level=9; fix all errors before proceeding. Run vendor/bin/phpunit or vendor/bin/pest; enforce 80%+ coverage. Only deliver when both pass clean.Load detailed guidance based on context:
| Topic | Reference | Load When |
|---|---|---|
| Modern PHP | references/modern-php-features.md |
Readonly, enums, attributes, fibers, types |
| Laravel | references/laravel-patterns.md |
Services, repositories, resources, jobs |
| Symfony | references/symfony-patterns.md |
DI, events, commands, voters |
| Async PHP | references/async-patterns.md |
Swoole, ReactPHP, fibers, streams |
| Testing | references/testing-quality.md |
PHPUnit, PHPStan, Pest, mocking |
declare(strict_types=1))Every complete implementation delivers: a typed entity/DTO, a service class, and a test. Use these as the baseline structure.
<?php
declare(strict_types=1);
namespace App\DTO;
final readonly class CreateUserDTO
{
public function __construct(
public string $name,
public string $email,
public string $password,
) {}
public static function fromArray(array $data): self
{
return new self(
name: $data['name'],
email: $data['email'],
password: $data['password'],
);
}
}
<?php
declare(strict_types=1);
namespace App\Services;
use App\DTO\CreateUserDTO;
use App\Models\User;
use App\Repositories\UserRepositoryInterface;
use Illuminate\Support\Facades\Hash;
final class UserService
{
public function __construct(
private readonly UserRepositoryInterface $users,
) {}
public function create(CreateUserDTO $dto): User
{
return $this->users->create([
'name' => $dto->name,
'email' => $dto->email,
'password' => Hash::make($dto->password),
]);
}
}
<?php
declare(strict_types=1);
namespace Tests\Unit\Services;
use App\DTO\CreateUserDTO;
use App\Models\User;
use App\Repositories\UserRepositoryInterface;
use App\Services\UserService;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
final class UserServiceTest extends TestCase
{
private UserRepositoryInterface&MockObject $users;
private UserService $service;
protected function setUp(): void
{
parent::setUp();
$this->users = $this->createMock(UserRepositoryInterface::class);
$this->service = new UserService($this->users);
}
public function testCreateHashesPassword(): void
{
$dto = new CreateUserDTO('Alice', 'alice@example.com', 'secret');
$user = new User(['name' => 'Alice', 'email' => 'alice@example.com']);
$this->users
->expects($this->once())
->method('create')
->willReturn($user);
$result = $this->service->create($dto);
$this->assertSame('Alice', $result->name);
}
}
<?php
declare(strict_types=1);
namespace App\Enums;
enum UserStatus: string
{
case Active = 'active';
case Inactive = 'inactive';
case Banned = 'banned';
public function label(): string
{
return match($this) {
self::Active => 'Active',
self::Inactive => 'Inactive',
self::Banned => 'Banned',
};
}
}
When implementing a feature, deliver in this order:
PHP 8.3+, Laravel 11, Symfony 7, Composer, PHPStan, Psalm, PHPUnit, Pest, Eloquent ORM, Doctrine, PSR standards, Swoole, ReactPHP, Redis, MySQL/PostgreSQL, REST/GraphQL APIs
AsyrafHussin
Modern PHP 8.x patterns, PSR standards, type system best practices, and SOLID principles. Contains 51 rules for writing clean, maintainable PHP code.
github
Generate a complete PHP Model Context Protocol server project with tools, resources, prompts, and tests using the official PHP SDK.