note 130451 added to language.enumerations.static-methods

From: Date: Wed, 20 Aug 2025 04:36:55 +0000
Subject: note 130451 added to language.enumerations.static-methods
Groups: php.notes 
Request: Send a blank email to php-notes+get-222310@lists.php.net to get a copy of this message
A simple order status example <?php define('select_option', '请选择'); define('order_success', '成功'); define('order_failed', '失败'); define('order_pending', '待支付'); function lang($key, $default='') { return defined($key) ? constant($key) : $default; } // 订单状态示例 enum OrderStatus: string { case pending = '200'; case success = '201'; case failed = '202'; /** * 这里可本地化 */ public function label(): string { return match($this) { self::success => lang('order_success'), self::failed => lang('order_failed'), self::pending => lang('order_pending'), }; } public static function forSelect(array $option = [], string $default = ''): array { $options = array_map(fn($status) => [ 'label' => $status->label(), 'value' => $status->value, 'default' => ($status->value === $default) ], self::cases()); return array_merge([$option], $options); } public function isSuccess(): bool { return $this === self::success; } } var_export(OrderStatus::forSelect(['label'=>lang('select_option'), 'value'=>''], '201')); // array var_export(OrderStatus::success->name); // 'success' var_export(OrderStatus::success->value); // '201' var_export(OrderStatus::success->label()); // '成功' var_export(OrderStatus::from('201')->label()); // '成功' $orderStatus = OrderStatus::from('201'); var_export($orderStatus->isSuccess()); // bool(true) ?> ---- Server IP: 45.112.84.4 Probable Submitter: 45.112.84.18 (proxied: 104.238.221.30) ---- Manual Page -- https://php.net/manual/en/language.enumerations.static-methods.php Edit -- https://main.php.net/note/edit/130451 Del: integrated -- https://main.php.net/note/delete/130451/integrated Del: useless -- https://main.php.net/note/delete/130451/useless Del: bad code -- https://main.php.net/note/delete/130451/bad+code Del: spam -- https://main.php.net/note/delete/130451/spam Del: non-english -- https://main.php.net/note/delete/130451/non-english Del: in docs -- https://main.php.net/note/delete/130451/in+docs Del: other reasons-- https://main.php.net/note/delete/130451 Reject -- https://main.php.net/note/reject/130451 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#222310) next »