Asymmetric Visibility

Public to read, restricted to write — PHP 8.4 public private(set) against read/write splits in 10 languages.

C C23

C23
#include <stdio.h>

/* C has no access control: every struct field is readable and writable by
   anyone with the definition — "private" survives only as a naming
   convention (the trailing underscore) and reviewer discipline */
typedef struct {
    int hits_;
} Counter;

void counter_record(Counter *c) { c->hits_++; }

int main(void) {
    Counter c = {0};
    counter_record(&c);
    counter_record(&c);
    printf("%d\n", c.hits_);
    c.hits_ = 99; /* nothing stops this — only the convention objects */
    printf("%d\n", c.hits_);
    return 0;
}
Output
2
99

C++ C++26

C++26
#include <cstdio>

// C++ private fields block outside writes entirely: a read/write split
// does not exist, so a public getter plus a private field is the idiom
class Counter {
public:
    void record() { ++hits_; }
    int hits() const { return hits_; }

private:
    int hits_ = 0;
};

int main() {
    Counter c;
    c.record();
    c.record();
    std::printf("%d\n", c.hits());
    // c.hits_ = 99; // will not compile: hits_ is private
}
Output
2

C# v11.0.100

v11.0.100
var c = new Counter();
c.Record();
c.Record();
Console.WriteLine(c.Hits);
// c.Hits = 99; // will not compile: the setter is private

class Counter {
    // the exact model PHP 8.4's public private(set) mirrors
    public int Hits { get; private set; } // public read, private write

    public void Record() { Hits++; }
}
Output
2

Go v1.27.4

v1.27.4
package main

import "fmt"

// Go's only fence is the package boundary: an unexported field is writable
// anywhere inside the package, so "readable outside, writable inside"
// has no direct equivalent — just an unexported field plus methods
type Counter struct {
	hits int
}

func (c *Counter) Record()   { c.hits++ }
func (c *Counter) Hits() int { return c.hits }

func main() {
	c := &Counter{}
	c.Record()
	c.Record()
	fmt.Println(c.Hits())
	// c.hits = 99 // won't compile outside the package
}
Output
2

Java v27.0.1

v27.0.1
public class Main {
    static class Counter {
        private int hits = 0; // no read/write split: a private field plus
                              // a lone getter is the convention
        void record() { hits++; }
        int hits() { return hits; }
    }

    public static void main(String[] args) {
        Counter c = new Counter();
        c.record();
        c.record();
        System.out.println(c.hits());
    }
}
Output
2

JavaScript v26.13.1

v26.13.1
"use strict";
class Counter {
  #hits = 0;
  record() { this.#hits++; }
  get hits() { return this.#hits; } // getter only: no setter defined
}

const c = new Counter();
c.record();
c.record();
console.log(c.hits);
try {
  c.hits = 99; // strict mode rejects the write
} catch (e) {
  console.log(e.constructor.name);
}
Output
2
TypeError

PHP v8.6.0

v8.6.0
class Counter {
    // PHP 8.4 asymmetric visibility: public read, private-set write;
    // short form private(set) means the same as public private(set)
    public private(set) int $hits = 0;

    public function record(): void {
        $this->hits++; // inside the class: writing is allowed
    }
}

$c = new Counter();
$c->record();
$c->record();
echo $c->hits, "\n"; // reading: allowed everywhere

try {
    $c->hits = 99; // outside write -> Error
} catch (Error $e) {
    echo get_class($e), ": ", $e->getMessage(), "\n";
}
Output
2
Error: Cannot modify private(set) property Counter::$hits from global scope

Python v3.15.2

v3.15.2
class Counter:
    def __init__(self):
        self._hits = 0  # underscore = private by convention

    def record(self):
        self._hits += 1

    @property
    def hits(self):  # read-only half: no @hits.setter defined
        return self._hits

c = Counter()
c.record()
c.record()
print(c.hits)
try:
    c.hits = 99
except AttributeError:
    print("write rejected: read-only property")
Output
2
write rejected: read-only property

Rust v1.99.1

v1.99.1
// pub fields follow module visibility; a read/write split does not
// exist, so the idiom is a private field plus accessor methods
struct Counter {
    hits: i32,
}

impl Counter {
    fn record(&mut self) { self.hits += 1; }
    fn hits(&self) -> i32 { self.hits }
}

fn main() {
    let mut c = Counter { hits: 0 };
    c.record();
    c.record();
    println!("{}", c.hits());
}
Output
2

TypeScript v26.13.1

v26.13.1
"use strict";
class Counter {
  #hits = 0;
  record(): void { this.#hits++; }
  get hits(): number { return this.#hits; } // getter only: no setter
}

const c = new Counter();
c.record();
c.record();
console.log(c.hits);
try {
  c.hits = 99; // strict mode rejects the write
} catch (e) {
  console.log((e as Error).constructor.name);
}
Output
2
TypeError

Language differences

C: no access control (example): struct fields are readable and writable by anyone holding the definition, and privacy survives only as an underscore naming convention; nothing blocks a direct external write.

C++: private fields are all-or-nothing (example): both external reads and writes are blocked, with a const-reference getter as the read-only exit — a read/write split must be simulated with a getter plus a private field; there is no field-level syntax for public-read/restricted-write.

C#: { get; private set; } (example): the direct blueprint for PHP 8.4 asymmetric visibility, matching word for word; C# also supports { get; protected set; } and init (writable only during initialization). The difference: C# reads and writes share one property, while PHP's set visibility can also apply to virtual properties and hooks.

Go: coarse export granularity (example): the only fence is the package boundary and an unexported field is writable anywhere inside it; readable-outside, writable-inside has no field-level support — only an unexported field plus exported methods. The granularity is exactly two levels: in-package or out.

Java: no language-level support (example): the convention is a private field plus a lone getter with no setter; there is no syntax for a publicly readable field with restricted writing — everything rests on encapsulation conventions; Lombok's @Getter(AccessLevel.NONE) is compile-time generation.

JavaScript: getter-only properties (example): with only a get accessor defined, strict mode throws TypeError on assignment while non-strict silently fails — the half-public behavior hinges on the strict switch and amounts to read-only rather than graded writes; there is no protected(set)-style middle ground.

PHP: 8.4+ asymmetric visibility (example): three tiers — public private(set), public protected(set), protected private(set); set visibility may not be wider than the main visibility (protected public(set) is a syntax error); the short private(set) form equals public private(set); private(set) properties are automatically final (cannot be redeclared in subclasses); since 8.5 the set visibility also applies to static properties; taking a reference follows the set visibility, since a reference can bypass the write check.

Python: the @property read-only half (example): omitting the setter makes assignment throw AttributeError — you get fully read-only rather than graded writes; there is no language-level public-read/private-write granularity, and the underscore prefix convention runs on honor.

Rust: pub fields at module granularity (example): a pub field is readable and writable outside the module, with no read/write split; outside-read-only needs a private field plus a pub getter; the granularity is two levels, and pub(crate)/pub(super) move the fence without ever splitting read from write.

TypeScript: at the language level the same getter-only properties as JS (example): the type system's private/readonly are compile-time markers — readonly fields are assignable only at declaration/construction (stricter than writable-inside-the-class), with no protected(set)-style runtime grading; at runtime the behavior is identical to JS.