Property Hooks

Properties whose reads and writes run code — PHP 8.4 property hooks against accessors across 10 languages.

C C23

C23
#include <stdio.h>
#include <string.h>

/* C has no properties: getters and setters are plain functions around a
   struct field, and every access goes through call syntax */
typedef struct {
    char name[32];
    int writes;
} User;

void user_set_name(User *u, const char *v) {
    strncpy(u->name, v, sizeof u->name - 1);
    u->writes++;
}

int main(void) {
    User u = {"", 0};
    user_set_name(&u, "Ada");
    printf("%s (%d)\n", u.name, u.writes);
    return 0;
}
Output
Ada (1)

C++ C++26

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

// C++ has no property hooks: accessors are hand-written methods that
// wrap a private field, and every read goes through the call syntax
class User {
public:
    void set_name(const std::string &v) {
        writes_++;
        name_ = v;
    }
    const std::string &name() const { return name_; }
    int writes() const { return writes_; }

private:
    std::string name_;
    int writes_ = 0;
};

int main() {
    User u;
    u.set_name("Ada");
    std::printf("%s (%d)\n", u.name().c_str(), u.writes());
}
Output
Ada (1)

C# v11.0.100

v11.0.100
var u = new User();
u.Name = "  Ada  ";
Console.WriteLine(u.Name);
Console.WriteLine(u.Writes);

class User {
    private string _name = "";
    private int _setCount;

    // the C# accessor model PHP 8.4 hooks mirror most directly
    public string Name {
        get => _name;
        set {
            _setCount++;          // a side effect in the accessor
            _name = value.Trim(); // normalize on the way in
        }
    }

    public int Writes => _setCount;
}
Output
Ada
1

Go v1.27.4

v1.27.4
package main

import "fmt"

// Go has no property hooks: getters/setters are plain methods, and the
// convention is bare X() for reads and SetX() for writes
type User struct {
	name   string
	writes int
}

func (u *User) SetName(v string) {
	u.name = v
	u.writes++
}

func (u *User) Name() string { return u.name }
func (u *User) Writes() int  { return u.writes }

func main() {
	u := &User{}
	u.SetName("Ada")
	fmt.Println(u.Name(), u.Writes())
}
Output
Ada 1

Java v27.0.1

v27.0.1
public class Main {
    static class User {
        private String name = "";
        private int writes = 0;

        // Java has no property hooks: accessor methods are the only route
        void setName(String v) { name = v; writes++; }
        String getName() { return name; }
        int getWrites() { return writes; }
    }

    public static void main(String[] args) {
        User u = new User();
        u.setName("Ada");
        System.out.println(u.getName() + " (" + u.getWrites() + ")");
    }
}
Output
Ada (1)

JavaScript v26.13.1

v26.13.1
class User {
  #name = "";
  #writes = 0;

  get name() { return this.#name; }
  set name(v) {
    this.#writes++;        // a side effect in the setter
    this.#name = v.trim(); // normalize on the way in
  }
  get writes() { return this.#writes; }
}

const u = new User();
u.name = "  Ada  ";
console.log(u.name);
console.log(u.writes);
Output
Ada
1

PHP v8.6.0

v8.6.0
class User {
    private int $setCount = 0;

    // PHP 8.4 property hooks: read and write intercept in place — callers
    // keep using plain $u->name syntax while the hooks run
    public string $name {
        set {
            $this->setCount++;          // side effect: count the writes
            $this->name = trim($value); // store into the backing value
        }
    }

    // virtual property: no storage, derived on read
    public int $writes {
        get => $this->setCount;
    }
}

$u = new User();
$u->name = "  Ada  ";
echo $u->name, "\n";
echo $u->writes, "\n";
$u->name = " Grace ";
echo $u->name, "\n";
echo $u->writes, "\n";

class Rect {
    public function __construct(public float $w, public float $h) {}

    public float $area {
        get => $this->w * $this->h; // virtual: computed on read
    }
}

$r = new Rect(3, 4);
echo $r->area, "\n";
try {
    $r->area = 30; // no set hook defined -> Error
} catch (Error $e) {
    echo get_class($e), ": ", $e->getMessage(), "\n";
}
Output
Ada
1
Grace
2
12
Error: Cannot write to get-only virtual property Rect::$area

Python v3.15.2

v3.15.2
class User:
    def __init__(self):
        self._name = ""
        self.writes = 0

    @property
    def name(self):
        return self._name

    @name.setter
    def name(self, value):
        self.writes += 1           # a side effect inside the setter
        self._name = value.strip() # normalize on the way in

u = User()
u.name = "  Ada  "
print(u.name)
print(u.writes)
Output
Ada
1

Rust v1.99.1

v1.99.1
// no property hooks: accessor methods around private fields are the idiom
struct User {
    name: String,
    writes: i32,
}

impl User {
    fn set_name(&mut self, v: &str) {
        self.writes += 1;
        self.name = v.trim().to_string();
    }
    fn name(&self) -> &str { &self.name }
    fn writes(&self) -> i32 { self.writes }
}

fn main() {
    let mut u = User { name: String::new(), writes: 0 };
    u.set_name("Ada");
    println!("{} ({})", u.name(), u.writes());
}
Output
Ada (1)

TypeScript v26.13.1

v26.13.1
class User {
  #name = "";
  #writes = 0;

  // typed accessors: the annotation lives on the property declaration
  get name(): string { return this.#name; }
  set name(v: string) {
    this.#writes++;        // a side effect in the setter
    this.#name = v.trim(); // normalize on the way in
  }
  get writes(): number { return this.#writes; }
}

const u = new User();
u.name = "  Ada  ";
console.log(u.name);
console.log(u.writes);
Output
Ada
1

Language differences

C: no properties (example): getters/setters are plain functions around a struct field (user_set_name), and every access is an explicit function call; no language mechanism can make a field access trigger code.

C++: no property hooks (example): a private field plus hand-written accessor methods; operator overloading can fake properties via libraries (proxy objects overloading operator=), but that is library magic — the language has no get/set interception.

C#: property accessors (example): get/set accessors are built into the language and are the direct blueprint for PHP 8.4 hooks — { get => _name; set { ... } } maps almost word for word onto PHP's { get; set { ... } }. Differences: C#'s setter uses the value keyword while PHP uses $value (renamable), and PHP has virtual properties (hooks that never touch backing storage cost zero memory) while every C# auto-property has a backing field.

Go: no property hooks (example): the convention is a bare X() for reads and a SetX() prefix for writes; property syntax was proposed but never adopted — access is always a method call and cannot masquerade as a field.

Java: no property hooks (example): the getX()/setX() method convention rules; records are the exception (header parameters straight into read-only fields), but mutable fields have no mechanism to trigger logic on assignment; Lombok's @Getter/@Setter is compile-time code generation, not language semantics.

JavaScript: get/set accessors (example): supported on both object literals and classes, semantically the same source as PHP hooks — reads trigger get, assignments trigger set. The difference: JS setters cannot declare a parameter type, while PHP's set hook can (and must be variance-compatible with the property type).

PHP: 8.4+ property hooks (example): get and set hooks hang off the property declaration inside {}; arrow shorthands get => ... and set => ... (the set expression's value becomes the written value); a hook referencing $this->name makes the property backed, otherwise it is virtual (zero storage, purely derived); a virtual property without a set hook throws Error on external assignment; hooks and readonly are mutually exclusive (use public private(set) to restrict access); isset/empty go through the get hook.

Python: the @property/@name.setter decorators (example): the getter takes @property alone, the setter adds @name.setter; omitting the setter makes an external assignment throw AttributeError — the same behavior as PHP's read-only virtual property. The difference: Python's property is a class-attribute object (overridable in subclasses, dynamically assemblable), while PHP hooks are syntax with zero-overhead inlining.

Rust: no property hooks (example): a private field plus accessor methods is the idiom; get-style attribute macros (getset and friends) generate boilerplate via procedural macros — build-time code generation; field access never triggers code, and the ownership model already polices reads (shared) and writes (exclusive) through the type system.

TypeScript: at the language level the same get/set accessors as JS (example); the difference is only the annotation on the declaration (get name(): string), from which the compiler derives read/write types; Node's native type stripping runs it directly, and transpiling below ES5 expands to Object.defineProperty.