Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:107965 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 60300 invoked from network); 3 Jan 2020 02:05:33 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 3 Jan 2020 02:05:33 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 08609180505 for ; Thu, 2 Jan 2020 16:09:34 -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=0.0 required=5.0 tests=BAYES_20,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 NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10olkn2063.outbound.protection.outlook.com [40.92.40.63]) (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 ; Thu, 2 Jan 2020 16:09:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Yu6xxUHCjZaVazdgbgGmlS7cXJWozwrifTEkeg1M2JxYbm9kHpe1xDdZoDNN6KLXF7FV5j58IBbio0sbbgt0k2O8Teq4gWv1E/s/3A6C8eIqTSbaygao9X3HlXSlde7cTnMAtU9Vg76t1yYWLFCeFnpA+pWKUb4knEP1wF7fyuruPlMrkpAkAk3kiTbnCdtYZLhyM/821ikEqOiUh9zXC2dqun5R8v7Tz+BH4+DMP4MDXTtRAD0kXlQ2es9Aj1N76ZpfKEYWk5gG6Qa1aX7MAeTWHWbszrNjuClMmv/Qt291bs6l4yw6PoS/OnOae/yWf7s9v+eqgJe4TTlpYc1yeQ== 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=f98tcbKd/9XVmpoFNs7+/71TNsYhNxGAUY3tRBCwMIY=; b=TuYMdUnHXCN/FN+4SFBYy8FtIsarfSoegSxTmwWh4PPZFjk/x3wJkkoAx5zqCyYxGDMb9fnq5HpQqWbVhrouDZ89F9TQFELD+7UwJHzD7YKCaXw1jOOnjBw/nONTMa2CTFkB2MbXE3+yGywqAUCZC6r9mDNGScZGO4r+zx/0XXO7zVEUjR6Dys+oHLaT7q6w0dPGDsSaGjyWbcVdet3t95G/kR2YzSE0KwJVUVgKGHEM2NDQio45e+2cEynKgELRA21LIGJ9BZyQ65yKI7n0th2wB90aRnQZT0M5V/mP/cCNsQfVs55pJyv7+B30HyaRP8Es07ZIG/fenPhQAbiALA== 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=f98tcbKd/9XVmpoFNs7+/71TNsYhNxGAUY3tRBCwMIY=; b=UJGN1dxq8ut9PX/QSW7E3QCEr50szglK5tbD+UUtJsXsbr/2pnBelqrwjp4g6fqPnDp1G2e0hwTL14Yp7KCdLAWYQc3WoP1WvV2TrGg8MUWEyAfGBtafhGjtaChpGri/wQM/q90sJnrbGHvZtFOVh6rlpJWJf4N0lmp2lU6GIBpOOpLaJvF0i9de4OiC7PdZ/1lulFwAm7BdQuB+NYAL/4DdEYXxIVOpM34qxSJf2k3b5+i5SSCMgmRwqovXC4OmQ1jBg1NnZvkhfdZxfaiG50zeyUVfvK64/w6pzC05ppFiJN1ETeBHxyWyZRvsRHhtAEfGqbDg1YxnlHM4vUcEYQ== Received: from DM6NAM10FT013.eop-nam10.prod.protection.outlook.com (10.13.152.60) by DM6NAM10HT220.eop-nam10.prod.protection.outlook.com (10.13.153.170) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2602.11; Fri, 3 Jan 2020 00:09:31 +0000 Received: from DM5PR07MB3067.namprd07.prod.outlook.com (10.13.152.56) by DM6NAM10FT013.mail.protection.outlook.com (10.13.152.142) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2602.11 via Frontend Transport; Fri, 3 Jan 2020 00:09:31 +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.012; Fri, 3 Jan 2020 00:09:31 +0000 To: Mike Schinkel , PHP internals Thread-Topic: [PHP-DEV] Initializing constants once, with code? Thread-Index: AQHVwZMGVGGpEwzQ00OYU/q+965QOqfYCRxe Date: Fri, 3 Jan 2020 00:09:31 +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:3A88860A023E03B98E0338DBEA65B52D3126D86900849964A1FABBA82035A0EB;UpperCasedChecksum:053981720B723A1F9D7422301B1B49EA998DB402B9CF875FC6D491C403F01E11;SizeAsReceived:7166;Count:45 x-ms-exchange-messagesentrepresentingtype: 1 x-tmn: [f/sq/0aIiYUuBKkiuOzh0P6XwjuAahyxCM+wTozlW8/G/f3gMc9d1QOplWfdVD6p] x-ms-publictraffictype: Email x-incomingheadercount: 45 x-eopattributedmessage: 0 x-ms-office365-filtering-correlation-id: 29e33059-6fa3-4f6d-b8e2-08d78fe13524 x-ms-traffictypediagnostic: DM6NAM10HT220: x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: 0t98VpUXRo9z5Bez3eh7Q1udgQaDde5gGtQamy9ri2wNNpY2aGt8Cf7ObkhHSxPBKjvVhNihDmE9pJTvG/fOhnkpSj58rv/koZSQwqd/55sY+M8KJljqz6uAZe+ztz3wmbW1xnCKrcOC2nsyhlQPBqPGpFfMCl9cBOiBgqxQgMLKV8uKIZ5xwUH0MAtItEVT 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: 29e33059-6fa3-4f6d-b8e2-08d78fe13524 X-MS-Exchange-CrossTenant-rms-persistedconsumerorg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-CrossTenant-originalarrivaltime: 03 Jan 2020 00:09:31.7057 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Internet X-MS-Exchange-CrossTenant-id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6NAM10HT220 Subject: Re: [PHP-DEV] Initializing constants once, with code? From: tysonandre775@hotmail.com (tyson andre) > The problem with using constants is that the value is still hardcoded=0A= > and if we later want to change to pulling the data from a config file =0A= > we have to change all the code that uses those constants.=A0=0A= =0A= It's possible to work around this by using a unique constant name=0A= and define('unique_name', $dynamicExpression)=0A= (and const MY_CLASS_CONST =3D \unique_name;)=0A= =0A= Maybe php could add something along these lines:=0A= =0A= local const X =3D sprintf('In %s', $dynamicValue);=0A= // Not supported in functions or loops.=0A= // Translates internally to:=0A= // if (defined('some_globally_unique_constant123af') || =0A= // !define('some_globally_unique_constant123af', X)) { throw ...; }=0A= // use some_globally_unique_constant123af as X;=0A= class MyClass {=0A= const PUBLIC =3D X;=0A= }=0A= =0A= - php still needs to prevent recursion such as =0A= `const A =3D count(B); const B =3D array_fill(1, A, 'value');`,=0A= which is why the simplest approach would be one that eagerly evaluates dy= namic expressions.=0A= That's probably not the only way - maybe it could track all of the consta= nts that=0A= are in the process of being defined and cause an uncatchable fatal error = if there was recursion.=0A= - Right now, reading the value of a class constant can already emit notices= or throw an Error (undeclared constants)=0A= the first time it gets evaluated=0A= - Any approach should throw for results that contain cycles, objects, or re= ferences,=0A= the same way define() warns.=0A= =0A= The below ugly proof of concept shows that the php runtime can in a sense= =0A= defer evaluation of dynamic class constants to runtime (not recursively)=0A= (assume errstr/errfile control what constant gets loaded).=0A= This POC should not be used anywhere.=0A= =0A= [[][0], SOME_GLOBALLY_UNIQUE_CONSTANT][1],=0A= ];=0A= }=0A= $globalVar =3D 'world';=0A= echo "Fetching MyTest::FOO\n";=0A= var_dump(MyTest::FOO);=0A= /*=0A= Output:=0A= Fetching MyTest::FOO=0A= Ignoring 8: Undefined offset: 0=0A= array(1) {=0A= ["myKey"]=3D>=0A= string(12) "Hello, world"=0A= }=0A= */=0A=