• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

php语法中有let吗?

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

来源:http://stackoverflow.com/questions/9705281/with-and-let-in-php

 

use(&$a) 

用 use ($parameter) 这种语法来往一个函数里面传参。比如往一些回调函数里面传参,这是医用手段。

我们再继续深入一下 PHP中的 anonymous function , 体会一下 use 的用法 和 有 & 符号与没有 $ 符号的区别。

 

 

 

 

Recently I found out about with and let statements for javascript.

I'm interested in achieving something like mentioned here:http://stackoverflow.com/a/1462102/393406, except for PHP in OOP fashion.

Like:

$builder = new markupbuilder();
with($markupbuilder){
    div(
        h1('A title...') . 
        p('This is ' . span('paragraph') . ' here.')
    );
}

// respectively
$builder->div(
    $builder->h1('A title...') . 
    $builder->p('This is' . $builder->span('paragraph') . ' here')
);

  So, is there something similar for PHP or how could I achieve this in PHP?

Answer:

first:

This is sort of a trite reply, but you have not saved a single character of typing in your example, and I believe that's the whole point of using with. The with statement also introduces ambiguity. Do you want to call getElementsByTagName on the DOMDocument object, or do you want to call the globally defined function of the same name?

As for let, PHP variables apply to the scope they are in by default -- you have to use the globalkeyword not only to make them globally accessible in general but any time you want to use them in another scope (and I would suggest never to use them period). Some may say it is a limitation on PHP that if and other constructs don't have their own scope, and let may be useful there, but if you wanted another variable in one of those constructs you could just declare it separately or move the functionality to a function scope.

second:

PHP doesn't have let but you can accomplish the same thing using other constructs, thought it is much messier!

First, what we wish we could do: (This doesn't work)

echo '<pre>';
$a = 5;
for (let $i=0; $i<10; $i++) {
    var_dump($i*$a);
}
var_dump('done looping');
var_dump(isset($i));
exit;

  This is what we have to do instead:

echo '<pre>';
$a = 5;
$loop = function() use (&$a) {
    for ($i=0; $i<10; $i++) {
        var_dump($i*$a);
    }
};
$loop();
var_dump('done looping');
var_dump(isset($i));
exit;

  

$loop is assigned to an anonymous function which is actually a closure. Because $i is defined inside the function, it now has function scope and can't "leak". $a on the other hand could be passed to the function as a param, but this would get in the way if you wanted this function to have real parameters. Instead we give the function access to $a through the use statement.

Finally, we call $loop(); because PHP doesn't let us call anonymous functions the way JavaScript does function() { alert('My anonymous function!'); }();

We then call var_dump(isset($i)); and see that $i is not set outside of the function.


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
七牛---关于PHPSDK的各种Demo发布时间:2022-07-10
下一篇:
自上而下,逐步揭开PHP解析大整数的面纱发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap