<?php declare(strict_types=1); #[Attribute(Attribute::TARGET_PROPERTY)] class TestAttribute { public function __construct( public $callback ) {} } final class Clock { public function format(string $format): string { return $format; } } final class Example { // This version FAILS (Fatal error: Constant expression contains invalid operations) //#[TestAttribute(static fn (Clock $clock) => $clock->format('Y-m-d H:i:s'))] // This version PASSES #[TestAttribute(static function (Clock $clock) { return $clock->format('Y-m-d H:i:s'); })] public ?Clock $created_at = null; } $ref = new ReflectionProperty(Example::class, 'created_at'); $attr = $ref->getAttributes(TestAttribute::class)[0]->newInstance(); var_dump($attr->callback instanceof Closure);
You have javascript disabled. You will not be able to edit any code.