Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:107961 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 78307 invoked from network); 2 Jan 2020 19:31:18 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 2 Jan 2020 19:31:18 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id CF63A18053E for ; Thu, 2 Jan 2020 09:35:15 -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,HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-yw1-f46.google.com (mail-yw1-f46.google.com [209.85.161.46]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Thu, 2 Jan 2020 09:35:15 -0800 (PST) Received: by mail-yw1-f46.google.com with SMTP id x138so17481752ywd.9 for ; Thu, 02 Jan 2020 09:35:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=newclarity-net.20150623.gappssmtp.com; s=20150623; h=from:mime-version:subject:message-id:references:to:date; bh=OfL/SG69i+funRV2XsT78d4qnuil20kVXErY6V5m+qA=; b=Fzsmq6kG7EfgQJx2ez4yE2j90nBug0oQx1Am132I5WqBzQCvxK4bvNikXucPAgek7d oT0dZOSYZGfZBuaCAx+2VNjqvOhcvSjV9o7JGUuMiky/jshNIQZJ58+bJq4+/2PPUIVo nc3Hr4kiyaAw939SWBFK7vgskiS+S9BWSFIQw8Rtm/XDIKvwHWE5AMN4trX5+AU1nj/4 Pw+4NkumeUgzXi7mm24QwZm+OclkKGQ/KA65tczkSjB8QXr4vQTDyG4rUHWQ0/fbNkHs FLx2vbltR50CkTYvl0JRYjuvJYOJ3iw7azY7zZJnK3P5PUBPn4x6uFfGkjRH75LYspah 7NWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:mime-version:subject:message-id:references :to:date; bh=OfL/SG69i+funRV2XsT78d4qnuil20kVXErY6V5m+qA=; b=n8ZOWYfilGZyr56F5DAdeiQNb7VNPFQ94lpg0LXn5ttdG+9Wn0vlQ8AIoA2ByZ/68z YJk0bn//2gZyVC/y4mFHCVmT3raDaacE+NvGWiaPubG8R51qwufI5ft7RnIlG7mPUdoo hVeYq1Q91IQ5UHNmbweho1UoD4QsNMpfWk6HhXF4PWMO/x4qWPt8k8O+J0FElGWwRl+Q tXiwaLQTjRDs/kfzezgbHnDesi4E3b2Kr0lsLOJNI63Iq+9A+GsMRVkxZkylejPu7TXB ehpGPOp+nPHWTMb4mwo/EqJd93GIB40Q5cKzFYd6gwieQcWkV1NuspMvJWPQXom1WYe/ nllw== X-Gm-Message-State: APjAAAXEpwGaR7E1kPQzhNG4UPpcaPEoPRd1iPsaXqE2wVSF7FD9WFJL Uff9YIMkfuctL5aQKb/H/5x7mBRD3y+G7g== X-Google-Smtp-Source: APXvYqy7R/JkZK9xRdhPRKYu0+m6Bk0ZDfCOOQu2Ofscyze2DmX2/OmSSD7vGgs+hCQtKAKjg0DQeQ== X-Received: by 2002:a81:6c4c:: with SMTP id h73mr59696292ywc.489.1577986513760; Thu, 02 Jan 2020 09:35:13 -0800 (PST) Received: from ?IPv6:2601:c0:c680:5cc0:d041:f4cb:aa77:d91e? ([2601:c0:c680:5cc0:d041:f4cb:aa77:d91e]) by smtp.gmail.com with ESMTPSA id e185sm23073695ywa.33.2020.01.02.09.35.12 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 02 Jan 2020 09:35:12 -0800 (PST) Content-Type: multipart/alternative; boundary="Apple-Mail=_860CF432-EE08-469A-9490-7F1A6D2F529C" Mime-Version: 1.0 (Mac OS X Mail 12.4 \(3445.104.11\)) Message-ID: <74F2DBFC-E63C-428C-A37F-2D0CEE15AD0F@newclarity.net> References: <5e0d723f.1c69fb81.e2ae8.24e2SMTPIN_ADDED_MISSING@mx.google.com> To: PHP internals Date: Thu, 2 Jan 2020 12:35:11 -0500 X-Mailer: Apple Mail (2.3445.104.11) Subject: Initializing constants once, with code? From: mike@newclarity.net (Mike Schinkel) --Apple-Mail=_860CF432-EE08-469A-9490-7F1A6D2F529C Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 Happy New Year Internals! One of the areas I have been agonizing over in 2019 has been constants = =E2=80=94 especially class constants =E2=80=94 and Mark Randall's = following comment from the other thread cause me to remember that I had = been planned to ask the list to discuss for a while. > Begin forwarded message: > From: Mark Randall > Subject: [PHP-DEV] Re: [RFC] "use global functions/consts" statement > Date: January 1, 2020 at 11:31:48 PM EST > To: internals@lists.php.net >=20 > That doesn't cover defined constants, but IMO we should be pushing = those to be moved into class constants, simply because in the absense of = function / constant / anything-but-class level autoloading, things might = as well be in a readily available location. I have found namespaced class constants as a great usable syntax for = DRYing out code during refactoring related to typically hardcoded = literal values, or when writing code initially. Once such example might = be: namespace Example; class API { const BASE_URL =3D 'http://api.example.com'; } With this a developer can use like so: namespace Example; .... $api_url =3D sprintf( '%s/%s', API::BASE_URL, '/widgets' ); Instead of writing this 20 different places across a codebase: $api_url =3D 'http://api.example.com/widgets'; The problem with using constants is that the value is still hardcoded = and if we later want to change to pulling the data from a config file we = have to change all the code that uses those constants. =20 Yes we could use methods instead, but frankly constants feel more = natural for things that typically get hardcoded. We all know that constants need to be, by definition, constant (a.k.a. = immutable), but is there any reason we could not allow constants a = one-time initialization via code? That way we would allow a developer = to hardcode a constant and then later come back and convert that = constant to be initialized from a configuration file without having to = modify any code that uses the constant? If others agree with this concern then three (3) approaches come to = mind, either of which would satisfactory to me although the first would = be best IMO. That said, I am open to any approach that allows constants = to be one-time initialized with code. Approach #1: Extend the const keyword to allow function-like syntax Extend the constant keyword to support all the syntax supported by = methods except parameters, but only run the code the first time the = constant is accessed, on demand: namespace Example; class API { const BASE_URL:string { return Config::get_options('api_base_url'); } } $api_url =3D sprintf( '%s/%s', API::BASE_URL, '/widgets' ); Approach #2: Allow one-time assignment to a constant Alternately we could allow one-time assignment to a constant, although = this would be much less preferable because it would require the = initialization to be explicitly called by a developer, and the developer = would have to implement all their own on demand logic, or just = initialize all classes on page load which most people would probably = fall back to doing: namespace Example; class API { const BASE_URL; static function initialize() { self::BASE_URL =3D Config::get_options('api_base_url'); } } API::initialize(); $api_url =3D sprintf( '%s/%s', API::BASE_URL, '/widgets' ); Approach #3:Magic methods! A magic method could work too, but I know many others have reasons to = dislike them no the least of which is lack of static typing. I include = here more for completeness rather than as a serious suggestion: namespace Example; class API { static function __const(string $const_name) { $value =3D null; if ( 'BASE_URL' =3D=3D=3D $const_name ) { $value =3D Config::get_options('api_base_url'); } else { trigger_error( sprintf("constant %s::%s has not been defined.", get_called_class(), $const_name )); } return $value; } } $api_url =3D sprintf( '%s/%s', API::BASE_URL, '/widgets' ); Thank you for considering and I look forward to your thoughts. -Mike =20= --Apple-Mail=_860CF432-EE08-469A-9490-7F1A6D2F529C--