Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:107999 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 25984 invoked from network); 5 Jan 2020 23:59:52 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 5 Jan 2020 23:59:52 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 70B4C180511 for ; Sun, 5 Jan 2020 14:04:36 -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=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,SPF_HELO_PASS,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS8075 40.64.0.0/10 X-Spam-Virus: No X-Envelope-From: Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12olkn2043.outbound.protection.outlook.com [40.92.21.43]) (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 ; Sun, 5 Jan 2020 14:04:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=h2DJgaeSGejnmqOGWVHd0foji/g+UHSDXdXH7FRLettLGC8EzEN9zTtdURBhYMa5zWJRH/v//zysmz/j9cEtfIwPXzsO41RbZUhtc/c+gNnVeuhiO6hSmiMlGZKgMLoHSzMXTQ07ewaVfwfKSRM+znRbv8AMGbJyGAs6oOC6JzFNtXIpAzESN7vyIi5dBHGoZmwsvHkZ6bwp2hTBezcGqSN1wp4bqusammY5hawg0V8a15pVGSTOLb9b9exDCrCyK9CKXSSoJDAaeIHGBJkwR09pzK7Br1zRMq+YzO69+z9LVTZimVsfTtl5ue4UbdWHKEpskdrhcLcS4ZRbxL/t7w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=TnjqpNudeC7ibBpyXMsycX8Q9JXb09oDEMzWFT63P48=; b=ToETyV/MQdIA07ZNBywygvES12huuon3Lbe9z/dkzWIRCkUubsxblLXw6FsiPZIuA7FhPIs8byxnu5EDrQ7ykGSQsHr7VP9Zz3eFhw7x5mOm0HkkKrZpF4nwKDxwcm2jHeFcK8Et4n8Rg00w575CV33E7KYM/jAeRZGFK8XuwS/aZbyKc8JYcVk6GKG5l6fjrDYsSEFHFdIs3oCX/DSHxpE2dgpdFNaM8SQtMCNIJVIo0wujOuW12nFEbFcoHjo9/rRpA2iK6Szt+FnoAGKr5TKyd7HqyEGBreFvALzikwr+zJsFrt1L24Mu1SH9OlHfJL4eW8exj7TSmUpria2Nyw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=hotmail.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=TnjqpNudeC7ibBpyXMsycX8Q9JXb09oDEMzWFT63P48=; b=k8RsqAaBk0NNtQog4WsWVUT2Wdf8FDXKR5zmVDFTGcr6T1nNeoHgToQ2GG6dTmmRyRk08Aexd2PHYHi4YXtsQJ2ds7XlmnFtIMynPAR59WwrHgdQarJcEnqZXxb4GvZwirsmUCG0gYPpeHt4ixxynaq7jDZzq8uF0PsIeVdeVxmWP69jw2kHzTMZ0gVylepC2rh/LOuwChOxRRiXIgP953QVYaZWgGQIupxF324FF8FM65+cvHblKVdFCdh01JM4zrJBb/h3/lG1yVDVgus5B+7VBjmfxriZ1pAxO3rldT8VRTZUHqjY2QdyV+aOJ1gaz/3ADrp830w40lAq9ukYgQ== Received: from MW2NAM12FT062.eop-nam12.prod.protection.outlook.com (10.13.180.58) by MW2NAM12HT162.eop-nam12.prod.protection.outlook.com (10.13.181.103) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2623.4; Sun, 5 Jan 2020 22:04:34 +0000 Received: from DM5PR07MB3067.namprd07.prod.outlook.com (10.13.180.57) by MW2NAM12FT062.mail.protection.outlook.com (10.13.181.114) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2623.4 via Frontend Transport; Sun, 5 Jan 2020 22:04:34 +0000 Received: from DM5PR07MB3067.namprd07.prod.outlook.com ([fe80::29e3:53bf:163e:5beb]) by DM5PR07MB3067.namprd07.prod.outlook.com ([fe80::29e3:53bf:163e:5beb%3]) with mapi id 15.20.2602.015; Sun, 5 Jan 2020 22:04:34 +0000 To: Mike Schinkel , PHP internals Thread-Topic: [PHP-DEV] Initializing constants once, with code? Thread-Index: AQHVwZMGVGGpEwzQ00OYU/q+965QOqfcl9gC Date: Sun, 5 Jan 2020 22:04:34 +0000 Message-ID: References: <5e0d723f.1c69fb81.e2ae8.24e2SMTPIN_ADDED_MISSING@mx.google.com>,<74F2DBFC-E63C-428C-A37F-2D0CEE15AD0F@newclarity.net> In-Reply-To: <74F2DBFC-E63C-428C-A37F-2D0CEE15AD0F@newclarity.net> Accept-Language: en-CA, en-US Content-Language: en-CA X-MS-Has-Attach: X-MS-TNEF-Correlator: x-incomingtopheadermarker: OriginalChecksum:56027577153E6883A8E14C20797D07C61D918B3A036BB5FC8E3C91DC6D8E99F5;UpperCasedChecksum:DFBBF574458CCD05D76444542CB163FCB95B0C0CC8BB2044AB0F8C1880B80304;SizeAsReceived:7165;Count:45 x-ms-exchange-messagesentrepresentingtype: 1 x-tmn: [qbIs7DRivrHArJYezIJry6kdpdL0DV9Z/xf3iDuifi1CU39ORH1mV+JJ5S21XBQI] x-ms-publictraffictype: Email x-incomingheadercount: 45 x-eopattributedmessage: 0 x-ms-office365-filtering-correlation-id: 38df45e4-c344-49fa-8136-08d7922b3f6c x-ms-traffictypediagnostic: MW2NAM12HT162: x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: Mc4DCzr3HJ5ATm4UtaQTz4GRBJjCALki4Z5pcJmYkI5v9I4Lzt9s4f6WPN3bbZL5d5Jgg9KbhpYw9wlMYgU0UlQnVTKKJMlmQ0ogGyEU+v1R8tBXKAxPdzP3oFIMy4UfuWVqEVmjQtiuoCNc1hoxtnAsiqOdbf7XR9AQFxblKG0L49OJTOjm14dXVSLHldDh5xX5TwD2chreGchWYlkPb7mNnEHpTj17DuFSeRp6pXU= x-ms-exchange-transport-forked: True Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginatorOrg: hotmail.com X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-CrossTenant-Network-Message-Id: 38df45e4-c344-49fa-8136-08d7922b3f6c X-MS-Exchange-CrossTenant-rms-persistedconsumerorg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-CrossTenant-originalarrivaltime: 05 Jan 2020 22:04:34.1039 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Internet X-MS-Exchange-CrossTenant-id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW2NAM12HT162 Subject: Re: [PHP-DEV] Initializing constants once, with code? From: tysonandre775@hotmail.com (tyson andre) I have a work in progress wrapper for allowing const-like syntax for global= constants `static const X =3D $dynamicExpression;`.=0A= This can be seen in https://github.com/TysonAndre/php-src/pull/10=0A= This is basically a wrapper around if (!defined(...)) { define(...); }=0A= If anyone's planning to implement changes to the way constants can be decla= red,=0A= then the POC might be worth looking at (e.g. to see what parts of the code = would need to be changed),=0A= even if their design and approach is different.=0A= =0A= - The issues this solves has overlap and differences with what Mike Schinke= l requests here.=0A= It's intended as a solution to limitations in different use cases that I'= ve encountered/thought about (which this thread reminded me of).=0A= =0A= Notably, this deliberately can't refer to other *dynamic* "static const" = values that haven't been declared.=0A= - This deliberately limits it to the top-level statements in files=0A= - Class constants below the global 'static const' can then use the eagerly = evaluated const.=0A= =0A= Reasons for me to implement the proof of concept this way:=0A= - defined()/define() is already supported by the engine.=0A= This is a slight improvement due to shorter syntax,=0A= and being more certain the const is only set or used in one file.=0A= - Eagerly initialized dynamic constants are easier to reason about in some = ways (e.g. fetching the constant of an already fetched class is less likely= to cause pauses or exceptions.)=0A= This makes un-noticed errors in rare code paths less likely.=0A= - Attempting to support function calls, property accesses, class constant a= ccesses (e.g. inherited), and variables in class constants recursively and/= or lazily=0A= would probably break a lot of code/assumptions in the php engine.=0A= =0A= I might be mistaken about that, though - I'd be glad if it turned out to = be easy to implement.=0A= =0A= Declaring 'static const' as a top level statement of a function/closure/met= hod might also be possible, but would require more work.=0A= =0A= ------=0A= =0A= It's also worth mentioning that some expressions that definitely have const= ant values=0A= can't be written (in a convenient way) because they use syntax that is prev= ented right now, such as function calls, closures, etc.=0A= =0A= My preference would be to make it easier to use dynamic expressions, and re= commend in coding guidelines that=0A= certain types of expressions "SHOULD NOT" be used for constants that vary o= n a particular host.=0A= (network calls, time, etc)=0A= =0A= ```=0A= const DEFAULT_PAYLOAD =3D json_encode(['key' =3D> 'value']);=0A= const ALLOWED_ELEMENT_TYPES =3D array_merge(self::FIRST_TYPES, self::SECOND= _TYPES);=0A= const ELEMENT_LENGTHS =3D array_map('strlen', self::ELEMENTS);=0A= ```=0A= =0A= =0A= =0A= > For derive-once global constants, you may be looking for define.=0A= > That's a runtime operation and effectively boils down to a set-once globa= l.=0A= > It doesn't help with constants defined in a class,=0A= > but for an already-global constant define already gives you a one-time se= t operation,=0A= > just not on-demand.=0A= =0A= This POC adds an alternate syntax for a set-once global.=0A= It prevents accidentally reusing the same global name in multiple files (e.= g. with copy and paste),=0A= and lets people looking at it know the constant is only used in that file.= =0A= =0A= >=A0Another use-case that just occurred to me is to support testing:=0A= > =0A= > class Api {=0A= > const URL: string{=0A= > return $_ENV['EXAMPLE_API_URL'] ?? 'https://api.example.com';=0A= > }=0A= > }=0A= =0A= That example would be written with the POC as:=0A= =0A= static const API_URL =3D $_ENV['EXAMPLE_API_URL'] ?? 'https://api.example.c= om';=0A= =0A= class Api {=0A= const URL =3D API_URL;=0A= }=