constant

constant(string $name): mixed

Returns the value of a constant given its name (a runtime reflection lookup for constants).

C C23

C23
#include <stdio.h>
#include <string.h>
#define APP_VERSION "1.0"

int main(void) {
    const char *name = "APP_VERSION";
    // macros vanish after preprocessing and never enter the symbol table; a hand-written mapping is the only way
    if (strcmp(name, "APP_VERSION") == 0) {
        printf("%s\n", APP_VERSION);
    } else {
        printf("?\n");
    }
    return 0;
}
Output
1.0

C++ C++26

C++26
#include <cstring>
#include <iostream>

// no constant reflection in C++; a hand-written mapping is the fallback here too
const char *constant(const char *name) {
    if (std::strcmp(name, "APP_VERSION") == 0) return "1.0";
    return "?";
}

int main() {
    std::cout << constant("APP_VERSION") << "\n";
    return 0;
}
Output
1.0

C# v11.0.100

v11.0.100
using System;
using System.Reflection;

var value = typeof(App).GetField("AppVersion")!.GetValue(null); // reflection can read const literal fields
Console.WriteLine(value);

static class App
{
    public const string AppVersion = "1.0";
}
Output
1.0

Go v1.27.4

v1.27.4
package main

import "fmt"

const AppVersion = "1.0"

func main() {
	// Go consts never enter the runtime symbol table; build your own mapping
	table := map[string]string{"AppVersion": AppVersion}
	fmt.Println(table["AppVersion"])
}
Output
1.0

Java v27.0.1

v27.0.1
import java.lang.reflect.Field;

public class Main {
    static final String APP_VERSION = "1.0";

    public static void main(String[] args) throws Exception {
        Field f = Main.class.getDeclaredField("APP_VERSION"); // read the static field by name via reflection
        System.out.println(f.get(null));
    }
}
Output
1.0

JavaScript v26.13.1

v26.13.1
// lexical scope names are not reflectable; a constants-table object is the JS idiom
const CONSTANTS = { APP_VERSION: "1.0" };
const name = "APP_VERSION";
console.log(CONSTANTS[name] ?? "?");
Output
1.0

PHP v8.6.0

v8.6.0
define("APP_VERSION", "1.0");
$name = "APP_VERSION";
echo constant($name), "\n", defined($name) ? "yes" : "no";
Output
1.0
yes

Python v3.15.2

v3.15.2
APP_VERSION = "1.0"
name = "APP_VERSION"
print(globals().get(name, "?"))  # globals() looks names up by string directly
Output
1.0

Rust v1.99.1

v1.99.1
// no runtime reflection in Rust; dispatch names by hand with match
fn constant(name: &str) -> Option<&'static str> {
    match name {
        "APP_VERSION" => Some(APP_VERSION),
        _ => None,
    }
}

const APP_VERSION: &str = "1.0";

fn main() {
    println!("{}", constant("APP_VERSION").unwrap());
}
Output
1.0

TypeScript v26.13.1

v26.13.1
// lexical scope names are not reflectable; constants table + index access
const CONSTANTS: Record<string, string> = { APP_VERSION: "1.0" };
const name = "APP_VERSION";
console.log(CONSTANTS[name] ?? "?");
Output
1.0

Language differences

C: macros are fully expanded at preprocessing and never enter the symbol table — looking a constant up by string has no language support, so a hand-written name→value mapping is the only option (example).

C++: constexpr values are fixed at compile time but likewise have no name reflection; a runtime lookup needs a hand-written mapping function (example); RTTI covers types, not named constants.

C#: true reflection exists — a const field compiles to a static literal field readable via GetField(...).GetValue(null) (example); beware cross-assembly inlining.

Go: consts are evaluated at compile time and never enter the runtime symbol table, and reflection cannot reach them; the idiomatic substitute is a self-maintained name→value table (example).

Java: reflection is complete — getDeclaredField plus Field.get(null) reads a static constant field by name (example); primitives/strings of static final are inlined at use sites, yet reflection still reads the field's original value.

JavaScript: const/let lexical bindings are invisible to reflection — module-scope variables cannot be reached by string name; the idiomatic substitute is collecting constants into an indexable object table (example).

PHP: constant is the runtime reflection entry for constants — the name may be any string expression, usually paired with defined for an existence pre-check (example); querying an undefined constant throws an Error.

Python: module-level names are dictionary entries, so globals()[name] looks them up by string directly (example) — Python's 'reflection' is the strongest here by nature.

Rust: no runtime reflection; name-based dispatch must be hand-written with match (example), and constants are already inlined at use sites at compile time.

TypeScript: runtime matches JavaScript (lexical bindings are not reflectable); a constants table with Record indexing is the idiomatic substitute (example); types exist only at compile time.