Ask HN: A PHP Superset Language
by anvius | 2025-04-01 08:02:12+00:00
I'm developing a PHP transpiler and I've created a superset called Simple PHP Superset (SPS). The goal is not to replace PHP but to extend it—making the code easier to read and maintain while preserving full PHP compatibility. Our company’s codebase suffers from numerous issues, and I believe that SPS can help solve these by introducing modern improvements without breaking existing PHP libraries.

Here are the most features of SPS:

- Full Compatibility with PHP 8:

You can mix traditional PHP code (with braces, semicolons, etc.) with SPS syntax. In strict mode, warnings are emitted if you don’t follow the recommended style.

- Minimalist, Clean Syntax:

+ Use `end` instead of braces to close blocks. + Instantiate classes by calling them like functions (no need for `new`). + Use `raise(...)` in place of `throw new`.

- Private by Default:

All members (variables, functions, properties) are private by default. Use the keyword `pub` to expose them publicly.

- Immutable by Default:

+ Variables declared with `var` are immutable once assigned, and only variables declared with `mut` can be modified. + Function parameters intended to be modified must be marked as `mut` (transpiling to PHP by-reference).

- Strict Static Typing & Union Types:

You must declare explicit types (e.g. `int`, `string`) and use union types (e.g. `int|string`) for enhanced compile‑time safety.

- Modern Data Structures & Manipulation:

+ Slicing: Extract subarrays using the notation `[start:end]` (similar to Python). + Maps & JSON-like Objects: Use the `{ key: value }` notation to define maps and object literals, which is cleaner than PHP’s `=>`. + Destructuring: Easily extract values from arrays, tuples (using parentheses), and JSON-like objects (using braces).

- Named Parameters:

Functions can be called with named parameters in any order for improved clarity.

- Concurrency with Coroutines:

Launch concurrent tasks with `php(expr)`, which executes the expression in a Fiber (PHP 8.1–based). Use a scheduler (e.g. `Scheduler.joinAll()`) to wait for all coroutines to complete.

- Enhanced Functional Programming:

An unified standard library (`Standard\`) provides higher‑order functions like `map`, `filter`, and `reduce` in camelCase, with a logical parameter order. Also, most of PHP functions implemented with a logical order.

- System Types for Resources:

It includes classes like `System\File` to wrap system resources (files, sockets, streams) in an object‑oriented manner, providing methods like `read()`, `write()`, and `close()`, with automatic error handling.

- Composition with `compose`:

Emulating Go’s embedding, use `compose` to inject the behavior of a normal class into another. For example, `compose Logger` in a class will automatically create an internal field and delegate its public methods so you can call `this.log(...)` directly without referencing the internal variable.

- Multiple Return Values:

Functions can return multiple values via tuples, making it easier to communicate results without resorting to arrays or ad‑hoc structures.

I've designed SPS to modernize PHP by providing a cleaner (specially to avoid usual mistakes in our codebase), more expressive syntax along with stricter static typing, immutability by default, named parameters, slicing, JSON‑like maps and objects, and enhanced functional programming features—all while being fully compatible with existing PHP code. Wiping code of `<?php`. It also should introduce concurrency through coroutines based on PHP 8.1 Fibers and allows composition via `compose`, letting you embed complete class behavior directly into another class.

I'm actively developing the transpiler and would love to hear feedback from the community. Does this approach make sense? What aspects should I improve or change? Should I abondon it? Any input would be greatly appreciated!

Thank you!