get_object_vars
get_object_vars(object $object): array
Returns the object properties visible from the current scope - all three levels inside the class, public only outside; an object anatomy knife by visibility.
C C23
#include <stdio.h>
/* C struct members have no visibility: everything is readable
wherever the definition is visible */
typedef struct {
int x;
int y;
int secret;
} Point;
int main(void) {
Point p = { 1, 2, 3 };
printf("%d %d %d\n", p.x, p.y, p.secret); /* all three accessible */
return 0;
}
1 2 3
C++ C++26
#include <iostream>
class Point {
public:
int x = 1; // class members are private by default;
protected: // each label opens a section
int y = 2;
private:
int secret = 3;
public:
void showAll() const { std::cout << x << " " << y << " " << secret << "\n"; }
};
struct Plain { // struct flips the default to public
int open = 9;
};
int main() {
Point p;
p.showAll(); // inside: all three
std::cout << p.x << "\n"; // outside: the public section only
std::cout << Plain{}.open << "\n";
return 0;
}
1 2 3 1 9
C# v11.0.100
using System.Linq;
using System.Reflection;
class Point {
public int X = 1;
protected int Y = 2;
private int secret = 3;
// the inside view reads all three without any reflection:
public int PeekSecret() => secret;
}
class MainClass {
static void Main() {
// reflection mirrors get_object_vars' outside view:
// ask for the public instance fields and that is all you get
var fields = typeof(Point).GetFields(BindingFlags.Public | BindingFlags.Instance);
System.Console.WriteLine(string.Join(",", fields.Select(f => f.Name)));
// the inside view needs the NonPublic flag - PHP's scope switch
// becomes a filter-flag combination here; Ordinal keeps the sort
// locale-independent
var hidden = typeof(Point).GetFields(BindingFlags.NonPublic | BindingFlags.Instance)
.Select(f => f.Name).OrderBy(n => n, System.StringComparer.Ordinal);
System.Console.WriteLine(string.Join(",", hidden));
}
}
X Y,secret
Go v1.27.4
package main
import (
"fmt"
"reflect"
)
type Point struct {
X int // exported: visible outside the package
y int // unexported: package-private
secret int // still readable INSIDE the same package
}
func main() {
p := Point{1, 2, 3}
// Go visibility is per-package, not per-object: all fields are
// readable here (same package). reflect can list only exported ones:
t := reflect.TypeOf(p)
for i := 0; i < t.NumField(); i++ {
f := t.Field(i)
if f.IsExported() {
fmt.Println(f.Name, reflect.ValueOf(p).Field(i).Int())
}
}
}
X 1
Java v27.0.1
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.stream.Collectors;
class Point {
public int x = 1;
protected int y = 2;
private int secret = 3;
}
class Main {
public static void main(String[] args) {
// reflection CAN list private fields, but reading one needs
// setAccessible(true); the ordinary outside view is public only
var publicNames = Arrays.stream(Point.class.getDeclaredFields())
.filter(f -> (f.getModifiers() & java.lang.reflect.Modifier.PUBLIC) != 0)
.map(Field::getName)
.collect(Collectors.toList());
System.out.println(publicNames); // [x]
System.out.println(new Point().x); // public read works
}
}
[x] 1
JavaScript v26.13.1
class Point {
x = 1; // public field
#secret = 3; // true private field (ES2022)
constructor() { this.y = 2; }
// even INSIDE the class, Object.entries skips #secret -
// private fields live outside the ordinary property system
ownEntries() { return Object.entries(this); }
}
const p = new Point();
console.log(p.ownEntries().length); // 2 - not 3!
console.log(Object.keys(p).join(",")); // x,y - #secret is invisible
2 x,y
PHP v8.6.0
class Point {
public int $x = 1;
protected int $y = 2;
private int $secret = 3;
// called inside the class: all three visibility levels
public function ownVars(): array { return get_object_vars($this); }
}
$p = new Point();
echo json_encode($p->ownVars()), "\n"; // inside: x, y, secret
echo json_encode(get_object_vars($p)), "\n"; // outside: public only
{"x":1,"y":2,"secret":3}
{"x":1}
Python v3.15.2
class Point:
def __init__(self):
self.x = 1 # public by convention
self._y = 2 # "protected" by convention only
self.__secret = 3 # name mangling: stored as _Point__secret
p = Point()
# vars() sees everything - Python privacy is a convention, not a wall
print(vars(p))
print(p._Point__secret) # the mangled name is still accessible
{'x': 1, '_y': 2, '_Point__secret': 3}
3
Rust v1.99.1
// Rust has no runtime reflection: "listing the fields" means
// writing them by hand. Visibility is enforced at compile time -
// private fields are unreadable from outside the module.
struct Point {
pub x: i32,
y: i32, // private outside this module
}
impl Point {
fn all_fields(&self) -> String {
// inside the module: everything is visible
format!("{} {}", self.x, self.y)
}
}
fn main() {
let p = Point { x: 1, y: 2 };
println!("{}", p.all_fields()); // inside: 1 2
println!("{}", p.x); // pub field readable anywhere
}
1 2 1
TypeScript v26.13.1
class Point {
x = 1; // public
private y = 2; // TS-only: erased at runtime!
#secret = 3; // true ES private field
ownKeys(): string[] { return Object.keys(this); }
}
const p = new Point();
console.log(p.ownKeys().join(",")); // x,y - #secret never listed
// p.y would be a COMPILE error; at runtime the field still exists:
console.log((p as any).y); // 2 - the "private" field is really there
x,y 2
Language differences
C: structs have no visibility concept (example) - a published header is a public contract; "private" is only emulated with opaque pointers + accessor functions.
C++: three-state section labels (example); class defaults to private, struct to public - the only difference; no reflection, so "listing properties" is hand-written or waits for the C++26 static reflection proposals; member pointers &Point::x can only name accessible members.
C#: GetFields(Public | Instance) mirrors the outside view (example); private/protected need the NonPublic flag to be listed; properties are a separate axis - GetProperties() is its own API.
Go: visibility is per-package, not per-class/object (example) - secret reads fine inside the same package, the biggest semantic gap from PHP/Java; reflect.Field.IsExported() is the official API (Go 1.17+).
Java: getDeclaredFields() lists everything, getModifiers() filters (example); reading private needs setAccessible(true) (may be denied under the module system); get_object_vars' "scope switch" maps to combining reflection filter flags.
JavaScript: Object.entries/keys contain only ordinary enumerable own properties - #private is absent even INSIDE the class (the example's ownEntries returns 2, not 3), unlike PHP's inside-sees-all; plain field properties are enumerable by default, Object.defineProperty can switch it off.
PHP: the core semantics are "visible from the current scope" (example): inside the class all three levels, outside public only; get_class_vars() is the class-level counterpart (static property view); dynamic properties (implicit creation deprecated in 8.2) are not declared properties but still appear in the result.
Python: vars(obj) is obj.__dict__, everything visible (example) - Python privacy is convention; __name triggers name mangling stored as _Class__name, guarding against accidents not access; the weakest end of the visibility spectrum.
Rust: no runtime reflection (the example lists fields by hand); visibility is compile-time enforced - y cannot even be NAMED outside the module, harder than PHP's "visible but scope-limited"; pub(crate)/pub(super) add gradients.
TypeScript: the teaching highlight - private is compile-time erasure (the example reads (p as any).y as 2 at runtime), #secret is real privacy (never enumerated); two "private" semantics coexist in one language - picking the wrong one is an incident.