On Thu, Jan 2, 2020, at 6:39 PM, Mike Schinkel wrote:
> > On Jan 2, 2020, at 7:09 PM, tyson andre <[email protected]> wrote:
> >
> >> 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.
> >
> > It's possible to work around this by using a unique constant name
> > and define('unique_name', $dynamicExpression)
> > (and const MY_CLASS_CONST = \unique_name;)
>
> I do appreciate the attempt to come up with something that is a workaround.
>
> That said, I definitely do not want to do is start using "clever" code
> in my implementations because — one day — someone else will have to
> maintain what I write.
>
> Really, I was just asking for a discussion of potentially allowing for
> one-time initialization via code for constants and via a
> straightforward, obvious syntax and standardized syntax.
>
> Effectively I am just asking for immutable static values in a class
> that can be access vis the <Class>::<ImmutableValue> syntax.
>
> -Mike
I agree with the use case, but not with the solution of piggybacking them onto
constants. It seems to me it would be much cleaner to just... build that into
a static method call with the derivation-and-caching logic behind the scenes.
It's not complicated code.
class Stuff
{
protected $val;
public static function val() {
self::$val ??= computeStuff();
return self::$val;
}
}
Although property accessors might make it even nicer looking, if we can ever
figure out how to make that performant. :-)
--Larry Garfield
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php