note 130566 added to language.attributes.syntax

From: Date: Sat, 06 Dec 2025 06:34:58 +0000
Subject: note 130566 added to language.attributes.syntax
Groups: php.notes 
Request: Send a blank email to php-notes+get-222446@lists.php.net to get a copy of this message
Attribute Subclassing https://3v4l.org/8jqK3#vnull <?php /** @author ciid */ #[Attribute(Attribute::TARGET_PROPERTY)] class PropertyAttributes { public function __construct( public readonly ?string $name = null, public readonly ?string $label = null, ) {} } #[Attribute(Attribute::TARGET_PROPERTY)] class IntegerPropertyAttributes extends PropertyAttributes { public function __construct( ?string $name = null, ?string $label = null, public readonly ?int $default = null, public readonly ?int $min = null, public readonly ?int $max = null, public readonly ?int $step = null, ) { parent::__construct($name, $label); } } #[Attribute(Attribute::TARGET_PROPERTY)] class FloatPropertyAttributes extends PropertyAttributes { public function __construct( ?string $name = null, ?string $label = null, public readonly ?float $default = null, public readonly ?float $min = null, public readonly ?float $max = null, ) { parent::__construct($name, $label); } } // model class with properties to edit in UI class MyClass { #[IntegerPropertyAttributes('prop','property: ',5,0,10,1)] public int $prop; #[FloatPropertyAttributes('float','double: ',5.5,0.0,9.9)] public float $double; } // index.php implementation $props = [['MyClass', 'prop'], ['MyClass', 'double']]; foreach ($props as $prop) { // class, property $refl = new ReflectionProperty($prop[0], $prop[1]); $reflAttrs = $refl->getAttributes(); foreach ($reflAttrs as $attr) echo buildHtmlFormControl($attr->newInstance()); } /** TL:DR: */ function buildHtmlFormControl(PropertyAttributes $args): string { $html = []; $html[] = "<label>{$args->label} <input name=\"{$args->name}\" value=\"{$args->default}\""; switch ($args::class) { case 'IntegerPropertyAttributes': $html[] = " type=\"number\" min=\"{$args->min}\" max=\"{$args->max}\" step=\"{$args->step}\""; break; case 'FloatPropertyAttributes': $html[] = " type=\"number\" min=\"{$args->min}\" max=\"{$args->max}\""; break; } $html[] = "></label>\n"; return implode('', $html); } ?> ---- Server IP: 206.189.2.9 Probable Submitter: 1.123.64.57 ---- Manual Page -- https://php.net/manual/en/language.attributes.syntax.php Edit -- https://main.php.net/note/edit/130566 Del: integrated -- https://main.php.net/note/delete/130566/integrated Del: useless -- https://main.php.net/note/delete/130566/useless Del: bad code -- https://main.php.net/note/delete/130566/bad+code Del: spam -- https://main.php.net/note/delete/130566/spam Del: non-english -- https://main.php.net/note/delete/130566/non-english Del: in docs -- https://main.php.net/note/delete/130566/in+docs Del: other reasons-- https://main.php.net/note/delete/130566 Reject -- https://main.php.net/note/reject/130566 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#222446) next »