> What happens if a function like strlen() is applied to a non-string
> argument? Conversion to string is certainly runtime-dependent even for
> primitive types like floats.

Good point. I thought that string casts were already allowed, but was mistaken.
It's possible to cast to string through concatenation right now, so it is a 
somewhat pre-existing issue.

```
php > const X = (string)2.3;
Fatal error: Constant expression contains invalid operations in php shell code 
on line 1
php > const Y = '' . 2.3;
php > var_export(Y);
'2.3'
```

Possible solutions:
1. Exclude functions with string parameters, strval(), strpos(), etc. from the 
limited set of functions in this RFC.

   Subsequent RFCs can be created to vote on for adding those groups of 
functions.
2. Always treat parameters in calls in constants like strict_types=1 - It's 
doable and won't break existing code, but I don't want to add that inconsistency
3. Keep existing behavior in PR

Currently, the implementation is intended to respect the strict_types setting 
of the file containing the constant expression, so strpos, strlen, etc would 
have that issue.
I still need to add a test that the implementation is actually doing that.

> Not sure I understand here - when these calls will be happening? And if
> you need a value that depends on runtime calculation, what's wrong with
> using define()?

The constant expressions will be evaluated at the same time php currently 
evaluates constant
expressions.
For class constants and static variable defaults, they're evaluated once at the 
time of use
For global constants, they're evaluated immediately.

If you want to use the define()d constant for a class constant, that is 
possible right now,
but it's an additional constant that's only (ideally) used in one place,
and there's the possibility of choosing conflicting names if the code is copied.

Another problem with using define() is that opcache cannot optimize global 
constants,
because define() will emit a notice instead of raising an Error if the constant 
already exists.
So the performance is slightly worse.

And the code is slightly less clear because of the indirection.
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to