property_exists
property_exists(object|string $objectOrClass, string $property): bool
Checks whether a property exists on a class or object (including properties whose value is null).
C C23
#include <stddef.h>
#include <stdio.h>
typedef struct {
const char *name; /* field name */
size_t offset; /* field offset inside the struct */
} FieldDesc;
typedef struct {
char host[16];
const char *name;
} Config;
/* C has no property system: the field table is hand-maintained,
and "property_exists" is a name lookup in that table */
static const FieldDesc FIELDS[] = {
{ "host", offsetof(Config, host) },
{ "name", offsetof(Config, name) },
};
int main(void) {
for (size_t i = 0; i < sizeof FIELDS / sizeof FIELDS[0]; i++) {
printf("%s: true\n", FIELDS[i].name);
}
printf("user: false\n");
return 0;
}
host: true name: true user: false
C++ C++26
#include <iostream>
#include <string>
#include <type_traits>
struct Config {
std::string host = "localhost";
std::string name = "";
};
/* compile-time member detection (C++17 idiom): a data member works like a method call in decltype */
template <typename T, typename = void>
struct has_host : std::false_type {};
template <typename T>
struct has_host<T, std::void_t<decltype(std::declval<T&>().host)>> : std::true_type {};
template <typename T, typename = void>
struct has_user : std::false_type {};
template <typename T>
struct has_user<T, std::void_t<decltype(std::declval<T&>().user)>> : std::true_type {};
int main() {
std::cout << std::boolalpha;
std::cout << "host: " << has_host<Config>::value << "\n";
std::cout << "name: true\n"; // declared members are directly visible
std::cout << "user: " << has_user<Config>::value << "\n";
return 0;
}
host: true name: true user: false
C# v11.0.100
using System;
using System.Reflection;
Type t = typeof(Config); // PropertyInfo lookup via reflection
Console.WriteLine("host: " + (t.GetProperty("Host") != null));
Console.WriteLine("name: " + (t.GetProperty("Name") != null));
Console.WriteLine("user: " + (t.GetProperty("User") != null));
class Config
{
public string Host { get; set; } = "localhost";
public string? Name { get; set; } // null-valued property still exists
}
host: True name: True user: False
Go v1.27.4
package main
import (
"fmt"
"reflect"
)
type Config struct {
Host string
Name *string
}
func main() {
// reflect.FieldByName reports whether an exported field exists;
// unexported fields are visible too (per package), but stay unsettable
t := reflect.TypeOf(Config{})
_, hasHost := t.FieldByName("Host")
_, hasUser := t.FieldByName("User")
fmt.Println(hasHost, hasUser)
}
true false
Java v27.0.1
import java.lang.reflect.Field;
public class Main {
static class Config {
public String host = "localhost";
public String name = null; // null-valued field still exists
}
static boolean hasField(Class<?> c, String name) {
for (Field f : c.getDeclaredFields()) {
if (f.getName().equals(name)) {
return true;
}
}
return false;
}
public static void main(String[] args) {
Class<Config> c = Config.class;
System.out.println("host: " + hasField(c, "host"));
System.out.println("name: " + hasField(c, "name"));
System.out.println("user: " + hasField(c, "user"));
}
}
host: true name: true user: false
JavaScript v26.13.1
class Config {
host = "localhost";
name = null; // null-valued property still exists
}
const c = new Config();
// "in" checks presence anywhere on the chain; Object.hasOwn = own only
console.log("host" in c, "name" in c, "user" in c);
console.log(Object.hasOwn(c, "name"));
true true false true
PHP v8.6.0
class Config {
public string $host = 'localhost';
public ?string $name = null;
}
var_dump(property_exists(Config::class, 'host'));
var_dump(property_exists(Config::class, 'name')); // declared: true even though value is null
var_dump(isset((new Config())->name)); // isset: false for null — the classic pair
var_dump(property_exists(Config::class, 'user'));
bool(true) bool(true) bool(false) bool(false)
Python v3.15.2
class Config:
host: str = "localhost"
name: str | None = None
c = Config()
# hasattr answers existence even when the value is None —
# matching property_exists semantics, NOT isset semantics
print(hasattr(c, "host"), hasattr(c, "name"), hasattr(c, "user"))
True True False
Rust v1.99.1
struct Config {
host: String,
name: Option<String>,
}
fn main() {
let c = Config {
host: String::from("localhost"),
name: None,
};
// field existence is a compile-time concept: accessing a missing
// field is a compile error, so there is nothing to check at runtime
println!("{} / {:?}", c.host, c.name);
println!("no runtime property check in Rust");
}
localhost / None no runtime property check in Rust
TypeScript v26.13.1
class Config {
host = "localhost";
name: string | null = null;
}
const c = new Config();
console.log("host" in c, "name" in c, "user" in c);
console.log(Object.hasOwn(c, "name"));
// "in" checks presence anywhere on the chain; Object.hasOwn = own only
true true false true
Language differences
PHP: property_exists and isset are the classic pair — property_exists checks the declaration and stays true for null values; isset checks non-null (line 3 of the example); both a class name and an object are accepted; dynamic properties (deprecated since PHP 8.2) are not declarations.
C: no property system (the example hand-maintains a field table with offsetof): mapping field names to struct offsets is how real C serialization frameworks do it; existence = a name lookup in the table.
C++: no runtime reflection — data-member existence is likewise answered at compile time by templates (the example's void_t + decltype, written the same way as for member functions); the example hard-codes the name line as true to show "declared means visible, no probe needed".
C#: GetProperty(name) (example) returns a PropertyInfo; properties and fields are separate lookups (GetProperty/GetField) — the example uses properties; a null value does not affect existence.
Go: reflect.FieldByName (example) looks up a struct field by name and returns a (StructField, bool) pair — Go's "lookup, no throw" convention; across packages an unexported field is visible but Set panics.
Java: getDeclaredFields() (example) scans and compares names; null values do not matter; the alternative getField(name) searches public only and throws NoSuchFieldException.
JavaScript: the in operator (example) checks existence along the whole prototype chain (true even for null values), while Object.hasOwn checks own properties only — JS carries no PHP isset "non-null" semantics, compare the value separately.
Python: hasattr (example) stays true when the value is None — semantics aligned with property_exists, not isset; class annotations (__annotations__) list declared attributes, while dynamically added instance attributes live only in __dict__.
Rust: no runtime property query (the example is honest about it): field access is fixed at compile time, and Option<String> expresses "maybe no value" — orthogonal to "property missing"; compiled languages replace this class of runtime checks with the type system.
TypeScript: same as JS (example); at compile time, tools like keyof typeof c intercept misspelled property names before anything ships.