Skip to content
Snippets·Free

PHP snippets

20 PHP 8 snippets: strict types, enums, readonly classes, constructor promotion, match, PDO with prepared statements, JSON, sessions, input validation, HTML escaping and PHPUnit tests. Some need PHP 8.1 or 8.2.

Free to install.

If PocketCode is installed on your Android device, this opens the page in the app.

Preview

5 of 20

Strict file headerstrict

Start a file with strict types and a namespace

<?php

declare(strict_types=1);

namespace ${1:App};

$0

Interfaceiface

Interface with one typed method

interface ${1:Repository}
{
    public function ${2:all}(): array;
}

Backed enumenumb

Enum whose cases map to strings (PHP 8.1+)

enum ${1:Status}: string
{
    case ${2:Active} = '${3:active}';
    case ${4:Inactive} = '${5:inactive}';
}

Composer autoloadautoload

Load the classes installed with Composer

require __DIR__ . '/vendor/autoload.php';

Traittraitb

Reusable methods to mix into several classes

trait ${1:HasTimestamps}
{
    $0
}

Changes in 1.0.0

First version.

Related