Open HallofFamer opened 9 years ago
"fixing the messy function names that cannot be touched due to backward compatibility issue".
Great point.
+1000
@alanwillms Nope, the current VM implementation doesn't allow this. With enough effort it might be possible, but would probably have negative performance impact for all other method calls (which is pretty much a no-go).
What is the limitation, why the scalar value can not be passed as a variable by reference?
@mvorisek Consider something like $a[$b][$c]->push($d)
. We need to know that $a[$b][$c]
is an array in order to determine that the call should be performed by-reference. However, we already need to convert $a
, $a[$b]
and $a[$b][$c]
into references before we know about that. The only way to make this work is to always fetch by reference, which would both result in a semantic change for existing object accesses, and in a large performance regression.
Supporting something like $a->push($b)
is simple, but supporting the general case is, I believe, semantically impossible.
@nikic $a[$b][$c]->push($d)
is good example.
As $a[$b][$c]
can be an instance (and the push
can be a regular method then), PHP must still wait until the expression is executed and then it can pass the resulting element ($a[$b][$c]
) by reference.
On the other side something like 1 ? $a[$b][$c]
: 0` (result from expression) should never be allowed to be modified by reference.
So this should be possible with methods that modified the original scalar object:
$a = [['x']];
$a->push('y');
$a[0]->push('y');
($a)->push('y'); // reset(($a)) works, so this should work too
(['x'])->push('y'); // it should work too, as the value is newly initialized and it is not a result from expression (i.e. not `1 ? ['x'] : 0`)
What do you think?
@nikic Also what do you think about a registration only for some namespace prefix(es)? This would widen the possibility of using the scalar objects in larger projects/packages a lot.
Hi! Just found this repo and it looks amazing. Any chance it gets on a RFC now? Maybe for PHP 9?
@brunoggdev See @nikic's last comment from 2019. I doubt much has changed in that regard.
I took a look at it and I very like what is being implemented. In my own framework I use classes for Int, Float, String and Array, but sometimes it can be a bit tedious(like having to write new String("abc") than just "abc"). This library will fix a lot of issues in my codebase, and make coding 10x more enjoyable. Also if its implemented at the PHP core, there's a chance to suggest a way to make even more elegant syntax work, such as "abc"->toUpper() which does not happen yet. So, will you consider moving this to RFC and possibly have PHP internals implement it in PHP 7?