Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

又被PHP坑了一小下 #38

Open
hax opened this issue Sep 22, 2016 · 0 comments
Open

又被PHP坑了一小下 #38

hax opened this issue Sep 22, 2016 · 0 comments
Labels

Comments

@hax
Copy link
Owner

hax commented Sep 22, 2016

通常变量名的构成都是有限制的,比如要避开keyword。
但PHP有个小trick,你可以用 ${'any string can be used'} = true;

于是我最近在改进编译jedi到PHP的代码时,就用了这个方式。

不料就踩坑了……

${'test'} = 'this is ok';
$x = function () use ($test) {}; // this is ok...
$x = function () use (${'test'}) {}; // this is NOT ok...

爆PHP语法错误。

这到底是为啥呢……

---- 无辜的分割线 ----

---- 无辜的分割线 ----

---- 无辜的分割线 ----

---- 无辜的分割线 ----

---- 无辜的分割线 ----

---- 无辜的分割线 ----

---- 无辜的分割线 ----

原来这个trick的本源就是“变量变量”(饶舌,好吧,合理的翻译是“可变变量”),比如:

$a = 'b';
$b = 'WTF';
echo $$a;

$$a${'test'} 其实都只是${expression}的特例而已。

use(...) 构造中只接受真正的 symbol 从实现的角度考虑倒也是可以理解的。不过从一致性的角度考虑,稍微花点力气让 use(...) 支持可变变量也不是做不到。只能说PHP一贯坑,这个真也不算什么了。

BTW,亚一程有个 use 别名的提案(https://wiki.php.net/rfc/useas ),但是貌似弃坑了。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant