class_alias

class_alias(string $class, string $alias, bool $autoload = true): bool

Creates a runtime alias for a defined class (internal classes, interfaces and traits since 8.3) - the alias IS the same class, not a subclass.

C C23

C23
#include <stdio.h>

/* C's alias is a COMPILE-time synonym: typedef names the same type */
typedef struct Widget {
    int id;
} Gadget;

int main(void) {
    struct Widget a = {1};
    Gadget b = a; /* the same type under two names, freely interchangeable */
    printf("%d\n", (int)sizeof(a) == (int)sizeof(b));
    printf("%d\n", a.id == b.id);
    return 0;
}
Output
1
1

C++ C++26

C++26
#include <iostream>
#include <type_traits>

class Widget {};
using Gadget = Widget; // compile-time alias: one type, two names

int main() {
    std::cout << std::is_same_v<Widget, Gadget> << "\n"; // true
    std::cout << (typeid(Widget) == typeid(Gadget)) << "\n"; // same RTTI entry
    return 0;
}
Output
1
1

C# v11.0.100

v11.0.100
using Gadget = System.Text.StringBuilder; // compile-time using alias

class MainClass {
    static void Main() {
        var a = new Gadget();
        // the alias names the SAME type: identity, not a new class
        System.Console.WriteLine(a.GetType() == typeof(System.Text.StringBuilder));
        System.Console.WriteLine(a.Length);
    }
}
Output
True
0

Go v1.27.4

v1.27.4
package main

import "fmt"

type Widget struct{ id int }

// Go 1.9 type alias: a second NAME for the same type, compile-time only
type Gadget = Widget

func main() {
	var a Widget = Widget{1}
	var b Gadget = a // interchangeable
	fmt.Println(b.id)
	fmt.Printf("%T\n", b) // the underlying type name survives: main.Widget
}
Output
1
main.Widget

Java v27.0.1

v27.0.1
import java.util.ArrayList;
import java.util.List;

// Java has NO alias mechanism: import only resolves simple names,
// it never creates a second name for a type
class Main {
    public static void main(String[] args) {
        List<String> b = new ArrayList<>(); // plain import, not an alias
        java.util.ArrayList<String> a = (java.util.ArrayList<String>) b;
        System.out.println(a.getClass().getSimpleName());
        System.out.println(a == b); // the same object through two name paths
    }
}
Output
ArrayList
true

JavaScript v26.13.1

v26.13.1
// no alias keyword - but a const binding to a class IS a runtime alias:
// the same class object reachable through two names
class Widget {}
const Gadget = Widget;

const a = new Widget();
const b = new Gadget();
console.log(a instanceof Gadget); // true - same class through two names
console.log(b instanceof Widget);
console.log(a.constructor === Widget);
console.log(Gadget.name); // classes keep their ORIGINAL name
Output
true
true
true
Widget

PHP v8.6.0

v8.6.0
// runtime class alias - rare among mainstream languages
class Widget {
    public int $n = 0;
}

var_dump(class_alias('Widget', 'Gadget'));
$a = new Widget();
$b = new Gadget();
var_dump($a instanceof Gadget); // alias: the SAME class
var_dump($b instanceof Widget);
var_dump(get_class($b));        // the object's real name stays "Widget"
$b->n++;
var_dump($a->n);                // instances stay independent...
var_dump($b->n);                // ...only the CLASS identity is shared
Output
bool(true)
bool(true)
bool(true)
string(6) "Widget"
int(0)
int(1)

Python v3.15.2

v3.15.2
# assignment creates a second NAME bound to the same class object - the
# runtime alias, exactly like PHP's class_alias
class Widget:
    pass


Gadget = Widget

a = Widget()
b = Gadget()
print(isinstance(a, Gadget))  # true - same class through two names
print(isinstance(b, Widget))
print(Gadget.__name__)  # the class remembers its ORIGINAL name
Output
True
True
Widget

Rust v1.99.1

v1.99.1
// Rust aliasing is compile-time: a type alias is the same type
struct Widget;

type Gadget = Widget; // zero-cost synonym, erased after type checking

fn takes_widget(_: Widget) {}

fn main() {
    let a = Widget;
    let b: Gadget = a; // alias IS the original type
    takes_widget(b);   // accepted without any conversion
    println!("ok");
}
Output
ok

TypeScript v26.13.1

v26.13.1
// type alias is compile-time only (erased); a const binding is the
// runtime alias - same as JS
class Widget {}
type Gadget = Widget; // compile-time: interchangeable in type positions

const a: Gadget = new Widget(); // alias checked at compile time
console.log(a instanceof Widget);

const GadgetRuntime = Widget; // runtime binding: the same class object
console.log(new GadgetRuntime() instanceof Widget);
Output
true
true

Language differences

C: typedef is a compile-time synonym (example) - C can never create an alias at runtime; the type table ceases to exist after compilation.

C++: using X = Y (C++11) is equivalent to typedef, one type under two names (example); runtime type info (RTTI) does not distinguish an alias either.

C#: using Alias = Type is compile-time only (example); GetType() always reports the original type name at runtime.

Go: Go 1.9 added the true alias type T2 = T1 (before that, type T2 T1 defined a NEW type); %T still prints the original type name (example).

Java: NO alias mechanism at all - import is mere simple-name resolution (example); renaming means defining a new class (which changes instanceof semantics).

JavaScript: no alias keyword, but const B = A IS a runtime alias - one class object, two bindings (example), Gadget.name keeps the original.

PHP: THE core scarcity contrast - only PHP establishes aliases at runtime: conditional definition / compatibility layers are the classic use case (class_alias inside an if branch picks the implementation, the third parameter triggers autoload); alias != inheritance - same class, private members visible, get_class reports the original name (example); internal classes supported since 8.3; works for interfaces and traits too; class names are case-insensitive and aliases are registered lowercase; PHP 8.6 deprecation reminder: is/let/_ are reserved as future keywords and using them as a class alias is deprecated.

Python: Gadget = Widget is a runtime second binding (example), isomorphic to PHP; __name__ keeps the original; module-level idiom is import x as y.

Rust: type aliases are erased at compile time (example); use ... as ... likewise; no runtime class-alias concept.

TypeScript: type aliases are compile-time (first half of the example); the runtime alias is the same const binding as in JS (second half).