thp 23

Typed Hypertext Processor

A modern, consistent typed language for PHP.

A truly Static Type System

thp keeps track of the datatype of all variables, and allows you to have complex types and type inference.

type Person = {
    String name,
    String lastName,
    Int age,
}

Option[Array[Person]] persons = PersonManager::getAll()

print("There are {persons?.length ?? 0} persons registered!")

Make the PHP stdlib good

thp groups all global variables and function of PHP into modules, to allow easy access and organization.
Function names, parameters and return types are improved, and you can treat primitive types as objects.

val name = "John Doe"

val lastNamePos = name.indexOf("Doe")  // Instead of `strpos`
val letters = name.split("")  // Instead of `str_split` or `explode`

val notALetters = letters.filter { $ != "a" }  // Instead of `array_filter`

Sound null safety & Pattern Matching

All null values must be explicitly marked and handled, avoiding many errors, via the Option ADT.

Also, functions that return false as an error state now return an Option, and exceptions return a Result instead.

val colors = Array("red", "blue", "green")

val response = match colors.search("purple") {
    Some(_) -> "purple is a color!"
    None -> "purple is not a color"
}

print(response)
use PDO
use Globals::Env

val #(Some(dbUri), Some(dbUser), Some(dbPassword)) = #(
    Env::get("DB_URI"),
    Env::get("DB_USERNAME"),
    Env::get("DB_PASSWORD"),
)
else {
    die("All 3 db environment variables must be set.")
}

match PDO(dbUri, dbUser, dbPassword) {
    Ok(connection) -> { /* db operations */ }
    Err(pdoException) -> { /* handle exception */ }
}