Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112434 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 30352 invoked from network); 5 Dec 2020 20:52:16 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 5 Dec 2020 20:52:16 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 7AF35180511 for ; Sat, 5 Dec 2020 12:20:47 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, SPF_HELO_PASS,SPF_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from wout2-smtp.messagingengine.com (wout2-smtp.messagingengine.com [64.147.123.25]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sat, 5 Dec 2020 12:20:46 -0800 (PST) Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.west.internal (Postfix) with ESMTP id 804B0938 for ; Sat, 5 Dec 2020 15:20:45 -0500 (EST) Received: from imap26 ([10.202.2.76]) by compute4.internal (MEProxy); Sat, 05 Dec 2020 15:20:45 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm1; bh=LFWYOz rGREjQmwWGHnkh455Y+t0v6z2b9z7a3aJ98X0=; b=bBlpAdAUvUBaEDd9iCdIif dVqHlP3qmpb8K9I1ZP6yk1ljAWcyHRYeviJ9LuGmIqD6EqhqzCHe8QSDqrEUJ4k+ bdI6MOe95SdCSO0aoobUOKyGOe8qF84v1NZOmDO+Kgavyrv9JcdyWL/xJFoUA9Nd cTsYA2A5Xb+WGHbYXpgwuMK039WFA/lNgyFmveK7uSTBXJ7w2/B2vsZWzpO4NbEy G4rc0I5EAeTjRiW3WqczVqV1VZgLJbxhhrbtsawl88Y9WkXJ0c5iGjew/hbiPIAO PD8NHAYO+E9SBOROMb7x97K+lEhngJnGWumi4nE3WslTilj6gwNWEN9Nu2r8gh9g == X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedujedrudejtddgudeflecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpefofgggkfgjfhffhffvufgtsehttdertderredtnecuhfhrohhmpedfnfgr rhhrhicuifgrrhhfihgvlhgufdcuoehlrghrrhihsehgrghrfhhivghlughtvggthhdrtg homheqnecuggftrfgrthhtvghrnhepgeelgfekudeivddvteffueejffdthfejieevhefg ffekudevkedtvdelvddvffefnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpe hmrghilhhfrhhomheplhgrrhhrhiesghgrrhhfihgvlhguthgvtghhrdgtohhm X-ME-Proxy: Received: by mailuser.nyi.internal (Postfix, from userid 501) id B83C314200A2; Sat, 5 Dec 2020 15:20:44 -0500 (EST) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.3.0-622-g4a97c0b-fm-20201115.001-g4a97c0b3 Mime-Version: 1.0 Message-ID: In-Reply-To: <9EA6E41D-B41F-4DF7-9A11-225DE74CED14@mabe.berlin> References: <9EA6E41D-B41F-4DF7-9A11-225DE74CED14@mabe.berlin> Date: Sat, 05 Dec 2020 14:20:23 -0600 To: "php internals" Content-Type: text/plain Subject: Re: [PHP-DEV] [RFC] Enumerations From: larry@garfieldtech.com ("Larry Garfield") On Sat, Dec 5, 2020, at 1:21 PM, Marc Bennewitz wrote: > > * I often use metadata in enumerations and so I would be very > > interested to allow constants. > > > Could you give an example what you mean? Metadata on individual cases is > supported by methods, which map more cleanly to things like interfaces, > and the notion of each case as a singleton object, not a static class. > > > That said, I have a related question: can enum cases be used as the > *value* of constants? e.g.: > > class OldMaid { > public const SUIT = Suit::Spades; > public const VALUE = CardValue::Queen; > } At present, no. They're "just" objects, and you can't assign an object to a constant. Unfortunately I'm not sure how to enable that without making them not-objects, which introduces all sorts of other complexity. > I mean on mapping something to something else defined as a single assoc > array constant. > Something like: > > enum Role { > case User, > case Admin, > ... > } > > enum Action { > case Order_Edit, > case Order_Read, > > private const BY_ROLE = [ > Role::User => [self::Order_Read], > Role::Admin => [self::Order_Read, self::Order_Edit], > ]; > > public function isAllowed(User $user) { > return in_array($this, self::BY_ROLE[$user->role]); > } > } Because of their object-ness, I think you'd have to use a weak map defined at runtime: class AccessControl { private WeakMap $perms; public function __construct() { $this->perms = new WeakMap(); $this->perms[Role::User] = [Action::Order_Read, Action::Order_Edit]; $this->perms[Role::Admin] = [Action::Order_Read, Action::Order_Edit]; } public function isAllowed($user, $action): bool { return in_array($action, $this->perms[$user->role]); } } --Larry Garfield