Attributes and Annotations

How metadata is attached and who reads it — PHP 8 Attributes surveyed across 10 languages.

C C23

C23
#include <stdio.h>

/* C23 has a real attribute syntax, but attributes are compile-time hints
   only: there is no reflection to read them back at runtime */
[[deprecated("use hello2 instead")]]
static const char *hello(void) { return "hi"; }

static const char *hello2(void) { return "hi v2"; }

int main(void) {
    /* calling hello() would trigger a compiler warning, not a runtime event */
    printf("%s\n", hello2());
    return 0;
}
Output
hi v2

C++ C++26

C++26
#include <cstdio>

// C++11 standard attributes: compile-time hints, no runtime reflection.
// [[nodiscard]] makes ignoring the return value a compiler warning.
[[nodiscard]] const char *hello() { return "hi"; }

int main() {
    std::printf("%s\n", hello());
    (void)hello(); // the cast silences the nodiscard warning explicitly
    return 0;
}
Output
hi

C# v11.0.100

v11.0.100
using System;
using System.Reflection;

// attributes are plain classes; reflection reads them back on demand
var t = typeof(Controller);
var classRoute = t.GetCustomAttribute<RouteAttribute>();
Console.WriteLine($"class route: {classRoute?.Path}");
var m = t.GetMethod("Hello");
Console.WriteLine($"method route: {m?.GetCustomAttribute<RouteAttribute>()?.Path}");

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
class RouteAttribute : Attribute
{
    public string Path { get; }
    public RouteAttribute(string path) { Path = path; }
}

[Route("/api")]
class Controller
{
    [Route("/api/hello")]
    public string Hello() => "hi";
}
Output
class route: /api
method route: /api/hello

Go v1.27.4

v1.27.4
package main

import (
	"fmt"
	"reflect"
)

// Go has no attributes; struct tags are the idiomatic metadata channel,
// read back with reflection
type Controller struct {
	Name string `route:"/api"`
}

func main() {
	t := reflect.TypeOf(Controller{})
	f, _ := t.FieldByName("Name")
	fmt.Println("tag:", f.Tag.Get("route"))
	fmt.Println("kind:", t.Kind())
}
Output
tag: /api
kind: struct

Java v27.0.1

v27.0.1
import java.lang.annotation.*;
import java.lang.reflect.Method;

// RUNTIME retention makes the annotation readable via reflection
@Retention(RetentionPolicy.RUNTIME)
@interface Route {
    String value();
}

@Route("/api")
class Controller {
    @Route("/api/hello")
    public String hello() { return "hi"; }
}

public class Main {
    public static void main(String[] args) throws Exception {
        Class<Controller> c = Controller.class;
        System.out.println("class route: " + c.getAnnotation(Route.class).value());
        Method m = c.getMethod("hello");
        System.out.println("method route: " + m.getAnnotation(Route.class).value());
    }
}
Output
class route: /api
method route: /api/hello

JavaScript v26.13.1

v26.13.1
// JS has no attribute syntax; the idioms are metadata properties and
// side tables keyed by the function itself
function hello() { return "hi"; }
hello.route = "/api/hello";   // metadata rides on the function object

const routes = new Map();     // registry pattern: a side table
routes.set(hello, hello.route);
console.log("route:", routes.get(hello));
console.log("name:", hello.name);
Output
route: /api/hello
name: hello

PHP v8.6.0

v8.6.0
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD)]
class Route {
    public function __construct(public string $path) {}
}

#[Route('/api')]
class Controller {
    #[Route('/api/hello')]
    public function hello(): string { return "hi"; }
}

// attributes stay inert until reflection instantiates them
$r = new ReflectionClass(Controller::class);
$route = $r->getAttributes(Route::class)[0]->newInstance();
echo "class route: ", $route->path, "\n";

$mRoute = $r->getMethod('hello')->getAttributes(Route::class)[0]->newInstance();
echo "method route: ", $mRoute->path, "\n";

$names = [];
foreach ($r->getAttributes() as $a) { $names[] = $a->getName(); }
echo "class attributes: ", implode(",", $names), "\n";
Output
class route: /api
method route: /api/hello
class attributes: Route

Python v3.15.2

v3.15.2
# a decorator is the closest Python idiom: it wraps code and can carry metadata
def route(path):
    def wrap(fn):
        fn.route = path      # metadata rides on the function object itself
        return fn
    return wrap

@route("/api/hello")
def hello():
    return "hi"

print("route:", hello.route)
print("name:", hello.__name__)
Output
route: /api/hello
name: hello

Rust v1.99.1

v1.99.1
// attributes drive the compiler: derive expands to trait impls at compile
// time; there is no runtime reflection to read them back
#[derive(Debug)]
struct Route {
    path: &'static str,
}

fn main() {
    let r = Route { path: "/api/hello" };
    println!("{:?}", r);
    println!("path: {}", r.path);
}
Output
Route { path: "/api/hello" }
path: /api/hello

TypeScript v26.13.1

v26.13.1
// TS adds no runtime metadata either; types describe, they do not annotate
function hello(): string { return "hi"; }
const routes = new Map<Function, string>();
routes.set(hello, "/api/hello");
console.log("route:", routes.get(hello), "name:", hello.name);
// stage-3 decorators and legacy experimentalDecorators need a transpiler
Output
route: /api/hello name: hello

Language differences

C: C23 standard attributes like [[deprecated]]/[[nodiscard]] (example): compile-time hints, no runtime reflection; framework-style metadata takes macros plus a hand-built registry.

C++: standard attributes since C++11 (example): [[nodiscard]] turns an ignored return value into a compiler warning; no runtime reflection — reading metadata means code generation until static reflection (C++26) lands.

C#: the progenitor Attribute system (example): an Attribute subclass with AttributeUsage targeting, read back via Attribute.GetCustomAttribute, instantiable on demand — isomorphic to PHP Attributes.

Go: no attribute mechanism (example): struct tags are the idiomatic metadata channel, read back with reflect's Tag.Get; the json/xml encoders are driven entirely by them.

Java: annotations (example): declared with @interface, runtime-reflectable only with @Retention(RUNTIME); compile-time processing rides the separate APT annotation-processor track.

JavaScript: no attribute syntax (example): the idioms are properties riding on function objects or a side-table Map; the Stage 3 decorator proposal is still pending and no built-in runtime metadata reflection exists.

PHP: 8.0+ Attributes (example): #[...] syntax, attribute classes declared with #[Attribute] and a target, lazily instantiated via ReflectionAttribute::newInstance() — read back through reflection, deeply bound to annotation-driven frameworks (Symfony/Doctrine).

Python: decorators (example): they wrap and transform code objects, carrying metadata as function attributes (fn.route) or __annotations__; runtime-reflectable, but the semantics are code transformation rather than static labeling.

Rust: #[derive(...)] and attribute macros (example): expanded into code (trait impls, macro output) at compile time with zero runtime reflection — attributes feed the compiler, they are not data.

TypeScript: at the language level same as JS (example): types describe, they do not annotate; Stage 3 decorators and legacy experimentalDecorators both need a transpiler and are unavailable under Node's native stripping.