Hi internals,

https://wiki.php.net/rfc/calls_in_constant_expressions has been updated and 
moved to
"Under Discussion".

This proposes allowing function calls in constant declarations, static property 
defaults,
static variables, and parameter defaults.
It includes a secondary voting option to allow any function calls,
and not just a small set of whitelisted core functions.
Other expression types would continue to be forbidden in constant expressions 
(variables, method calls, etc)

Let me know if you have any comments on the RFC or implementation.

Earlier discussion can be found in https://externals.io/message/108343
("Planning an RFC to allow calls to global functions in constant expressions")

I made some updates to the RFC, implementation and the proposed whitelist of 
functions.
(in response to https://externals.io/message/108343#108427)

> > As you've already realized, the main problem here is the behavior for
> > functions that have side-effects or state. Currently we mostly get away
> > with the illusion that it doesn't matter when exactly constexpr
> > initializers are evaluated. Apart from the error location and (admittedly
> > quite a few) other details, you ostensibly can't distinguish whether the
> > expression is going to evaluated on declaration, on first access or on each
> > access.
>
> Good point - If PHP ends up supporting an option where **any** global 
> function can get called,
> It'd make sense to evaluate any expression *with a function call*
> every time for instance properties and parameter defaults
> (e.g. `public $id = generate_unique_int_id()`).
> (the optimizer can later optimize any functions where it won't be noticeable).
>
> I think that would require updates to my PR for caching,
> since immutable returned zvals (including false/true) get cached permanently.

I've completed the updates to parameter defaults.
Parameter defaults containing calls now don't get cached:
They always get evaluated every time the parameter isn't passed in.

I also excluded support for calls in instance property defaults from the RFC.
I'd want that feature, but the changes to PHP's internal representation of 
classes expand
the scope of this RFC too much for me.

- PHP currently evaluates all the property defaults and permanently caches them
  the first time a property gets accessed or an object gets instantiated.
  I'd probably have to change the internal class representation as well to fix 
that,
  and I don't know how many or what types of bugs/issues that would cause.

> That reminds me:
> I forgot about functions such as `compact()`, `extract()` and 
> `get_defined_vars()`.
> (and `func_num_args()`/`func_get_args()`/`func_get_arg()`).
> It looks like all of them check `zend_forbid_dynamic_call()` to throw,
> but I need to add tests and possibly flag the call as dynamic.

Update: The implementation already properly throws an Error.

Thanks,
- Tyson
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to