Tyhpdef Quick Reference

A condensed overview of Tyhpdef syntax — the declaration-only language for describing existing PHP code to the Tyhp compiler. Similar to TypeScript's .d.ts files. For full details, follow the link below each feature.

The Open Tag

Every .tyhpdef file starts with the <?tyhpdef tag. Supports namespaces (semicolon and braced styles) and use/import statements. Only declarations are allowed — no function bodies or executable code.

See full documentation →

<?tyhpdef

const string APP_NAME;
function myPhpFunction(string $input): int;

Variables and Constants

Declare global PHP variables and constants with types. Use as for aliasing. Top-level deprecated/obsolete work. Tyhpdef ?? is parsed but not applied; & is not a tyhpdef form.

See full documentation →

bool $debugMode;
string $legacy_app_name as $appName;

const string APP_ENV;
const int \MAX_LOOPS_ALLOWED as MAX_LOOPS;

Functions

Declare function signatures with typed parameters (including optional, reference, and variadic), generic type parameters, async functions, function aliases, and extension functions.

See full documentation →

function \array_map<T, U>(callable<T, U> $callback, array<T> $array): array<U>;
async function fetchUserData(int $userId): UserData;
function \array_key_exists as keyExists(int|string $key, array $array): bool;
function toCamelCase(extends string $str): string;

Interfaces

Declare PHP interfaces with fully typed method signatures, constants, inheritance (extends), generic type parameters with constraints, method overloads, and async methods.

See full documentation →

interface Repository<T> {
    public function find(int $id): ?T;
    public function save(T $entity): void;
}

interface Stream extends Readable, Writable {
    public function close(): void;
}

Classes

Declare PHP classes with properties, methods, constructors (with parameter promotion), constants, extends/implements, generics, trait usage, operator overloads, and class aliasing with as.

See full documentation →

class Collection<T> {
    public function add(T $item): void;
    public function map<U>(callable<T, U> $transform): Collection<U>;
}

class \Vendor\LongNamespace\SomeClass as ShortName {
    public function doWork(): void;
}

Enums

Declare unit enums (no backing value) and backed enums (string or int). Supports case values, methods, interface implementation, constants, aliasing, and deprecated cases.

See full documentation →

enum HttpMethod: string {
    case GET = "GET";
    case POST = "POST";
    case PUT = "PUT";
}

enum Permission: int implements \Stringable {
    case Read = 1;
    case Write = 2;
    public function __toString(): string;
}

Traits

Declare PHP traits with typed properties, methods, constants, abstract methods, and generic type parameters. Generic traits specify concrete type arguments when used in Tyhp code.

See full documentation →

trait Collection<T> {
    public function add(T $item): void;
    public function getItems(): array<T>;
}

// in Tyhp code:
use Collection<User>;

Structs

Value types backed by PHP associative arrays. Support required and optional properties, property aliases for non-identifier array keys, defaults, inheritance, nesting, and generics.

See full documentation →

struct UserProfile {
    string $name;
    ?string $bio;
    string $role = "user";
}

struct ApiResponse<T> {
    bool $success;
    ?T $data;
    ?string 'Reply-To' as $replyTo;
}

Type Aliases

Create named aliases for complex type expressions using the type keyword. Supports generic parameters with constraints. Allowed at root level or inside namespace blocks.

See full documentation →

type Scalar = int|float|string|bool;
type StatusCode = 200|301|302|404|500;
type Result<T, TError extends \Throwable> = T|TError;
type Predicate<T> = callable<T, bool>;

Namespaces

Both braced and semicolon-style namespace declarations. Multiple namespaces per file allowed. PHP items can be imported into a different tyhpdef namespace using leading-backslash fully-qualified names.

See full documentation →

namespace App\Models {
    class User { public int $id; public string $name; }
}

namespace EL {
    class \EmailerLib\Emailer {
        public function __construct(string $subjectLine): void;
    }
}

Import Aliases

Alias nearly anything with as — functions, classes, interfaces, traits, enums, properties, methods, constants, variables, and enum cases. Only the aliased name is available in Tyhp.

See full documentation →

class LegacyApi as Api {
    public string $usr_nm as $username;
    public function get_usr as getUser(int $id): ?User;
}

Overloaded Functions and Methods

Declare multiple signatures for the same function/method name to describe PHP functions with varying parameter types and return types. Supports static value type overloads, generic overloads, and async overloads.

See full documentation →

function \json_decode(string $json, true $assoc): array<string, mixed>;
function \json_decode(string $json, false $assoc): object;
function \json_decode(string $json, null $assoc = null): mixed;

public function find(int $id): ?User;
public function find(string $uuid): ?User;
public function find(array<string, mixed> $criteria): array<User>;

The deprecated and obsolete Keywords

deprecated produces a compiler warning but still compiles. obsolete produces a compiler error and blocks compilation. Both must appear before all other modifiers. In this alpha they are enforced on top-level functions, classes, interfaces, traits, enums, constants, and variables — not on class/interface members or enum cases.

See full documentation →

deprecated function \mysql_connect(string $server, string $user, string $pass): \mysqli|false;
obsolete function \md5(string $string, bool $binary = false): string;

Runtime Errors and Exceptions

When tyhpdef declarations don't match actual PHP code, runtime errors can occur. Declare exception hierarchies and document throws with @throws (documentation only in this alpha).

See full documentation →

class \InvalidArgumentException extends \LogicException {
    public function __construct(
        string $message = "",
        int $code = 0,
        ?\Throwable $previous = null
    ): void;
}

/** @throws \JsonException When JSON encoding fails */
function \json_encode(mixed $value, int $flags = 0, int $depth = 512): string|false;