又被PHP坑了一小下
- Dominant language
- HTML
- Stars
- 464
- Forks
- 27
- PR merge metrics
- No merged PRs in 30d
Description
通常变量名的构成都是有限制的,比如要避开keyword。
但PHP有个小trick,你可以用 `${'any string can be used'} = true;`。
于是我最近在改进编译[jedi](https://github.com/baixing/jedi)到PHP的代码时,就用了这个方式。
不料就踩坑了……
``` php
${'test'} = 'this is ok';
$x = function () use ($test) {}; // this is ok...
$x = function () use (${'test'}) {}; // this is NOT ok...
```
爆PHP语法错误。
这到底是为啥呢……
---- 无辜的分割线 ----
---- 无辜的分割线 ----
---- 无辜的分割线 ----
---- 无辜的分割线 ----
---- 无辜的分割线 ----
---- 无辜的分割线 ----
---- 无辜的分割线 ----
原来这个trick的本源就是[“变量变量”](http://php.net/manual/en/language.variables.variable.php)(饶舌,好吧,合理的翻译是“可变变量”),比如:
``` php
$a = 'b';
$b = 'WTF';
echo $$a;
```
而 `$$a` 和 `${'test'}` 其实都只是`${expression}`的特例而已。
`use(...)` 构造中只接受真正的 symbol 从实现的角度考虑倒也是可以理解的。不过从一致性的角度考虑,稍微花点力气让 `use(...)` 支持可变变量也不是做不到。只能说PHP一贯坑,这个真也不算什么了。
BTW,亚一程有个 use 别名的提案(https://wiki.php.net/rfc/useas ),但是貌似弃坑了。
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.