Typed Properties

Who catches the bad type first — PHP 7.4 typed properties against the core runtime-versus-compile-time check across 10 languages.

C C23

C23
#include <stdio.h>

/* member types are checked at compile time; a wrong assignment simply
   does not compile */
typedef struct {
    int count;
    const char *label;
} Counter;

int main(void) {
    Counter c = { 0, NULL };
    c.count = 41;
    c.count++;                 /* type rules are the compiler's job */
    c.label = "hits";
    printf("%d %s\n", c.count, c.label);
    /* c.count = "abc"; */    /* compile error: incompatible types */
    return 0;
}
Output
42 hits

C++ C++26

C++26
#include <cstdio>
#include <string>

class Counter {
public:
    int count = 0;
    std::string label;
};

int main() {
    Counter c;
    c.count = 41;
    c.count++;
    c.label = "hits";
    std::printf("%d %s\n", c.count, c.label.c_str());
    // c.count = "abc"; // compile error: no conversion from const char* to int
    return 0;
}
Output
42 hits

C# v11.0.100

v11.0.100
var c = new Counter();
c.Count = 41;
c.Count++;
c.Label = "hits";
Console.WriteLine($"{c.Count} {c.Label}");
// c.Count = "abc"; // compile error: cannot implicitly convert string to int

class Counter
{
    public int Count = 0;
    public string? Label = null;
}
Output
42 hits

Go v1.27.4

v1.27.4
package main

import "fmt"

type Counter struct {
	Count int
	Label string
}

func main() {
	c := Counter{}
	c.Count = 41
	c.Count++
	c.Label = "hits"
	fmt.Println(c.Count, c.Label)
	// c.Count = "abc" // compile error: cannot use "abc" (string) as int
}
Output
42 hits

Java v27.0.1

v27.0.1
public class Main {
    static class Counter {
        int count = 0;
        String label = null;
    }

    public static void main(String[] args) {
        Counter c = new Counter();
        c.count = 41;
        c.count++;
        c.label = "hits";
        System.out.println(c.count + " " + c.label);
        // c.count = "abc"; // compile error: incompatible types
    }
}
Output
42 hits

JavaScript v26.13.1

v26.13.1
// no type declarations exist: any property accepts any value, always
const c = { count: 0, label: null };
c.count = 41;
c.count++;              // number
c.label = "hits";       // string now, was null before
console.log(c.count, c.label);
c.count = "abc";        // silently re-typed: nothing checks
console.log(c.count, typeof c.count);
Output
42 hits
abc string

PHP v8.6.0

v8.6.0
class Counter {
    public int $count = 0;
    public ?string $label = null;
}

$c = new Counter();
$c->count = 41;
$c->count++;              // runtime type check on every assignment
$c->label = "hits";
echo $c->count, " ", $c->label, "\n";

// wrong type: rejected at assignment time
try {
    $c->count = "abc";    // a non-numeric string into an int property
} catch (TypeError $e) {
    echo "TypeError: ", $e->getMessage(), "\n";
}

// declared but uninitialized: reading it is an Error
class Deferred {
    public string $token;
}
$d = new Deferred();
try {
    echo strlen($d->token), "\n";
} catch (Error $e) {
    echo "Error: ", $e->getMessage(), "\n";
}
Output
42 hits
TypeError: Cannot assign string to property Counter::$count of type int
Error: Typed property Deferred::$token must not be accessed before initialization

Python v3.15.2

v3.15.2
class Counter:
    count: int = 0        # annotation records intent, checks nothing
    label: str | None = None

c = Counter()
c.count = 41
c.count += 1
c.label = "hits"
print(c.count, c.label)

# the annotations are introspectable at runtime
print("annotations:", Counter.__annotations__)

# enforcement is opt-in: a wrong type is stored without complaint
c.count = "abc"
print(c.count, type(c.count).__name__)
Output
42 hits
annotations: {'count': <class 'int'>, 'label': str | None}
abc str

Rust v1.99.1

v1.99.1
struct Counter {
    count: i32,
    label: Option<String>,
}

fn main() {
    let mut c = Counter { count: 0, label: None };
    c.count = 41;
    c.count += 1;
    c.label = Some("hits".to_string());
    println!("{} {}", c.count, c.label.as_deref().unwrap());
    // c.count = "abc"; // compile error: expected i32, found &str
}
Output
42 hits

TypeScript v26.13.1

v26.13.1
// the type layer is compile-time only: tsc rejects bad assignments, but no
// check survives into the executed JavaScript
class Counter {
  count = 0;
  label: string | null = null;
}
const c = new Counter();
c.count = 41;
c.count++;
c.label = "hits";
console.log(c.count, c.label);
// under Node's strip-only TS this line executes: the runtime never sees types
c.count = "abc"; // compile error under tsc: Type 'string' is not assignable
console.log(c.count, typeof c.count);
Output
42 hits
abc string

Language differences

C: the static-typing baseline (example): member types are locked at compile time and a bad assignment fails to build; there is no optional runtime typing — the compile-time check is the only line of defense.

C++: the static-typing baseline (example): member types are compile-enforced, with const and references adding stronger compile-time constraints; a wrong assignment is a compile error, not an exception.

C#: the static-typing baseline (example): property/field types are compile-enforced; nullable reference types (string?) pull 'maybe null' into the compile check too — one of the high marks of the static lineage.

Go: the static-typing baseline (example): struct field types are compile-enforced with no implicit conversions (even int to string is explicit); a bad assignment fails to build.

Java: the static-typing baseline (example): field types are compile-enforced; generics erase at runtime down to raw types, but the declared field type itself is immutable.

JavaScript: no notion of types (example): any property accepts any value at any moment, silently re-typed; typeof is the only runtime probe.

PHP: 7.4+ typed properties (example): assignment-time runtime checks that throw TypeError — the textbook specimen of runtime checking; reading an uninitialized typed property throws Error (distinct from null); under coercive typing numeric strings may be assigned to int/float, while declare(strict_types=1) demands exact matches.

Python: class annotations (example): __annotations__ is runtime-reflectable (that is the genuine runtime-side capability), yet the engine enforces nothing — wrong types settle in silently; enforcement means hand-rolled isinstance or Pydantic-style libraries.

Rust: the static-typing baseline (example): field types are compile-enforced with no implicit conversions; Option<String> bakes 'maybe absent' into the type system itself.

TypeScript: the erasure contrast (example): tsc blocks the bad assignment, but the transpiled output carries no check — the sample lets the 'impossible' assignment execute under Node's strip-only mode, showing the compile-time-versus-runtime divide directly.