Claude
Skills
Sign in
Back

php-expert

Included with Lifetime
$97 forever

Expert-level PHP development with PHP 8+, Laravel, Composer, and modern best practices

languagesphplaravelcomposersymfonyphpunitpsr

What this skill does


# PHP Expert

Expert guidance for modern PHP development including PHP 8+ features, Laravel framework, Composer dependency management, and PHP best practices.

## Core Concepts

### PHP 8+ Features
- Union types and mixed type
- Named arguments
- Attributes (annotations)
- Constructor property promotion
- Match expressions
- Nullsafe operator
- JIT compiler
- Fibers (PHP 8.1+)
- Readonly properties and classes

### Object-Oriented PHP
- Classes and objects
- Interfaces and abstract classes
- Traits
- Namespaces
- Autoloading (PSR-4)
- Type declarations
- Visibility modifiers

### Modern PHP
- Strict types
- Return type declarations
- Property type declarations
- Enums (PHP 8.1+)
- First-class callable syntax

## Modern PHP 8+ Syntax

### Constructor Property Promotion
```php
<?php

// Before PHP 8
class User {
    private string $name;
    private string $email;
    private int $age;

    public function __construct(string $name, string $email, int $age) {
        $this->name = $name;
        $this->email = $email;
        $this->age = $age;
    }
}

// PHP 8+ (constructor property promotion)
class User {
    public function __construct(
        private string $name,
        private string $email,
        private int $age,
    ) {}

    public function getName(): string {
        return $this->name;
    }
}

$user = new User('Alice', '[email protected]', 30);
```

### Named Arguments
```php
<?php

function createUser(
    string $name,
    string $email,
    int $age = 18,
    bool $admin = false,
): User {
    return new User($name, $email, $age, $admin);
}

// Named arguments (PHP 8+)
$user = createUser(
    name: 'Alice',
    email: '[email protected]',
    age: 30,
    admin: true,
);

// Skip optional parameters
$user = createUser(
    name: 'Bob',
    email: '[email protected]',
);
```

### Union Types and Mixed
```php
<?php

// Union types (PHP 8+)
function processValue(int|float $number): int|float {
    return $number * 2;
}

function findUser(int|string $identifier): ?User {
    if (is_int($identifier)) {
        return User::find($identifier);
    }
    return User::where('email', $identifier)->first();
}

// Mixed type (accepts any type)
function debugValue(mixed $value): void {
    var_dump($value);
}
```

### Match Expression
```php
<?php

// Old switch
switch ($status) {
    case 'pending':
        $message = 'Order is pending';
        break;
    case 'processing':
        $message = 'Order is being processed';
        break;
    case 'completed':
        $message = 'Order completed';
        break;
    default:
        $message = 'Unknown status';
}

// Match expression (PHP 8+)
$message = match ($status) {
    'pending' => 'Order is pending',
    'processing' => 'Order is being processed',
    'completed' => 'Order completed',
    default => 'Unknown status',
};

// Multiple conditions
$result = match ($value) {
    0, 1, 2 => 'Small',
    3, 4, 5 => 'Medium',
    default => 'Large',
};

// With expressions
$discount = match (true) {
    $customer->isPremium() && $order->total() > 1000 => 0.20,
    $customer->isPremium() => 0.15,
    $order->total() > 500 => 0.10,
    default => 0,
};
```

### Nullsafe Operator
```php
<?php

// Before PHP 8
$country = null;
if ($user !== null) {
    $address = $user->getAddress();
    if ($address !== null) {
        $country = $address->getCountry();
    }
}

// PHP 8+ nullsafe operator
$country = $user?->getAddress()?->getCountry();

// With default
$country = $user?->getAddress()?->getCountry() ?? 'Unknown';
```

### Attributes (Annotations)
```php
<?php

// Define attribute
#[Attribute]
class Route {
    public function __construct(
        public string $path,
        public string $method = 'GET',
    ) {}
}

// Use attribute
class UserController {
    #[Route('/users', method: 'GET')]
    public function index(): array {
        return User::all();
    }

    #[Route('/users/{id}', method: 'GET')]
    public function show(int $id): User {
        return User::findOrFail($id);
    }

    #[Route('/users', method: 'POST')]
    public function store(Request $request): User {
        return User::create($request->all());
    }
}

// Read attributes
$reflection = new ReflectionClass(UserController::class);
foreach ($reflection->getMethods() as $method) {
    $attributes = $method->getAttributes(Route::class);
    foreach ($attributes as $attribute) {
        $route = $attribute->newInstance();
        echo "{$route->method} {$route->path}\n";
    }
}
```

### Enums (PHP 8.1+)
```php
<?php

// Basic enum
enum Status {
    case Pending;
    case Processing;
    case Completed;
    case Cancelled;
}

// Usage
$status = Status::Pending;

function updateOrder(Order $order, Status $status): void {
    $order->status = $status;
    $order->save();
}

// Backed enums
enum OrderStatus: string {
    case Pending = 'pending';
    case Processing = 'processing';
    case Completed = 'completed';
    case Cancelled = 'cancelled';

    public function label(): string {
        return match($this) {
            self::Pending => 'Pending',
            self::Processing => 'Being Processed',
            self::Completed => 'Completed',
            self::Cancelled => 'Cancelled',
        };
    }

    public function color(): string {
        return match($this) {
            self::Pending => 'yellow',
            self::Processing => 'blue',
            self::Completed => 'green',
            self::Cancelled => 'red',
        };
    }
}

$status = OrderStatus::from('pending');
echo $status->label(); // 'Pending'
echo $status->value;   // 'pending'
```

### Readonly Properties and Classes
```php
<?php

// Readonly property (PHP 8.1+)
class User {
    public function __construct(
        public readonly string $id,
        public readonly string $email,
        public string $name,
    ) {}
}

$user = new User('123', '[email protected]', 'Alice');
$user->name = 'Bob'; // OK
// $user->email = '[email protected]'; // Error: readonly property

// Readonly class (PHP 8.2+)
readonly class Point {
    public function __construct(
        public int $x,
        public int $y,
    ) {}
}
```

## Laravel Framework

### Models
```php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\SoftDeletes;

class User extends Model
{
    use SoftDeletes;

    protected $fillable = [
        'name',
        'email',
        'password',
    ];

    protected $hidden = [
        'password',
        'remember_token',
    ];

    protected $casts = [
        'email_verified_at' => 'datetime',
        'is_admin' => 'boolean',
        'settings' => 'array',
    ];

    // Relationships
    public function posts(): HasMany
    {
        return $this->hasMany(Post::class);
    }

    public function roles(): BelongsToMany
    {
        return $this->belongsToMany(Role::class);
    }

    // Accessors (Laravel 9+)
    protected function name(): Attribute
    {
        return Attribute::make(
            get: fn (string $value) => ucfirst($value),
            set: fn (string $value) => strtolower($value),
        );
    }

    // Scopes
    public function scopeActive($query)
    {
        return $query->where('active', true);
    }

    public function scopeAdmins($query)
    {
        return $query->where('is_admin', true);
    }

    // Methods
    public function isAdmin(): bool
    {
        return $this->is_admin;
    }
}

// Usage
$users = User::active()->get();
$admins = User::admins()->get();
$user = User::with('posts', 'roles')->find(1);
```

### Controllers
```php
<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Requests\StorePostRequest;
use App\Http\Requests\UpdatePostRequest;
use App\Http\Resources\PostResource;
use App\Models\Post;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;

clas

Related in languages