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.
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.
<?tyhpdef
const string APP_NAME;
function myPhpFunction(string $input): int;
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.
bool $debugMode;
string $legacy_app_name as $appName;
const string APP_ENV;
const int \MAX_LOOPS_ALLOWED as MAX_LOOPS;
Declare function signatures with typed parameters (including optional, reference, and variadic), generic type parameters, async functions, function aliases, and extension functions.
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;
Declare PHP interfaces with fully typed method signatures, constants, inheritance (extends), generic type parameters with constraints, method overloads, and async methods.
interface Repository<T> {
public function find(int $id): ?T;
public function save(T $entity): void;
}
interface Stream extends Readable, Writable {
public function close(): void;
}
Declare PHP classes with properties, methods, constructors (with parameter promotion), constants, extends/implements, generics, trait usage, operator overloads, and class aliasing with as.
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;
}
Declare unit enums (no backing value) and backed enums (string or int). Supports case values, methods, interface implementation, constants, aliasing, and deprecated cases.
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;
}
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.
trait Collection<T> {
public function add(T $item): void;
public function getItems(): array<T>;
}
// in Tyhp code:
use Collection<User>;
Value types backed by PHP associative arrays. Support required and optional properties, property aliases for non-identifier array keys, defaults, inheritance, nesting, and generics.
struct UserProfile {
string $name;
?string $bio;
string $role = "user";
}
struct ApiResponse<T> {
bool $success;
?T $data;
?string 'Reply-To' as $replyTo;
}
Create named aliases for complex type expressions using the type keyword. Supports generic parameters with constraints. Allowed at root level or inside namespace blocks.
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>;
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.
namespace App\Models {
class User { public int $id; public string $name; }
}
namespace EL {
class \EmailerLib\Emailer {
public function __construct(string $subjectLine): void;
}
}
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.
class LegacyApi as Api {
public string $usr_nm as $username;
public function get_usr as getUser(int $id): ?User;
}
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.
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>;
deprecated and obsolete Keywordsdeprecated 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.
deprecated function \mysql_connect(string $server, string $user, string $pass): \mysqli|false;
obsolete function \md5(string $string, bool $binary = false): string;
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).
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;