来源: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.

php 语法中有 let 吗?的更多相关文章

  1. 简体中国版文档的Markdown语法

    Markdown文件 注意︰这是简体中国版文档的Markdown语法.如果你正在寻找英语版文档.请参阅Markdown︰ Markdown: Syntax. Markdown: Syntax 概述 哲 ...

  2. Markdown语法说明(详解版)

    ####date: 2016-05-26 20:38:58 tags: Markdown tags && Syntax ##Markdown语法说明(详解版)杨帆发表于 2011-11 ...

  3. Markdown语法手册

    Markdown 语法手册 Markdown 是一种轻量级标记语言,能将文本换成有效的XHTML(或者HTML)文档,它的目标是实现易读易写,成为一种适用于网络的书写语言. Markdown 语法简洁 ...

  4. Markdown 语法说明

    Markdown 语法说明 (简体中文版) / (点击查看快速入门) 概述 宗旨 Markdown 的目标是实现「易读易写」. 可读性,无论如何,都是最重要的.一份使用 Markdown 格式撰写的文 ...

  5. [开发笔记]-MarkDown语法

    马克飞象MarkDown在线编辑器 http://maxiang.info/?basic 1. H1--H6 相应数量的# 2. 底线形式 = H1(最高阶标题)和- H2(第二阶标题) 3. 段落和 ...

  6. markdown语法集锦

    参考:http://wowubuntu.com/markdown/#blockquote 1. 标题 # 一级标题 ## 二级标题 ### 三级标题 共六级标题 2. 列表 有序列表:1,2,3: 无 ...

  7. Markdown 语法说明 (简体中文版)

    http://wowubuntu.com/markdown/#editor 概述 宗旨 兼容 HTML 特殊字符自动转换 区块元素 段落和换行 标题 区块引用 列表 代码区块 分隔线 区段元素 链接 ...

  8. [转]Markdown 语法手册

    Markdown 是一种轻量级标记语言,能将文本换成有效的XHTML(或者HTML)文档,它的目标是实现易读易写,成为一种适用于网络的书写语言. Markdown 语法简洁明了,易于掌握,所以用它来写 ...

  9. .md文件 Markdown 语法说明

    Markdown 语法说明 (简体中文版) / (点击查看快速入门) 概述 宗旨 兼容 HTML 特殊字符自动转换 区块元素 段落和换行 标题 区块引用 列表 代码区块 分隔线 区段元素 链接 强调 ...

随机推荐

  1. 最大独立集 HDU 1068

    题目大意:有n个人,两个人之间有相互的关系,问最大的关系数目. 思路:n-(最大匹配数/2).因为这里给出的是n个人之间的两两关系 //看看会不会爆int!数组会不会少了一维! //取物问题一定要小心 ...

  2. C#生成随机汉字

    using System; using System.Text; namespace ConsoleApplication {     class ChineseCode     {          ...

  3. C++11 半同步半异步线程池的实现

    #include <list> #include <mutex> #include <thread> #include <condition_variable ...

  4. mysql 不能插入中文

    三.#vim /etc/mysql/my.cnf .(5.5以前系统)在[client]下面加入 default-character-set=utf8 在[mysqld]下面加入default-cha ...

  5. 如何查看自己电脑支持OpenGL core版本

    1. 起因: 红宝书上的例子在电脑上运行后没有效果,但是怎么也找不到原因,反复对看了书上的源码和代码发现没有任何问题,但是就是没有树上写的效果 2. 思路:查看函数的说明,这里推荐使用docs.gl, ...

  6. C++中的函数指针和函数对象总结

    篇一.函数指针函数指针:是指向函数的指针变量,在C编译时,每一个函数都有一个入口地址,那么这个指向这个函数的函数指针便指向这个地址.函数指针的用途是很大的,主要有两个作用:用作调用函数和做函数的参数. ...

  7. OpenGL------在Windows系统中显示文字

    增加了两个文件,showline.c, showtext.c.分别为第二个和第三个示例程序的main函数相关部分.在ctbuf.h和textarea.h最开头部分增加了一句#include <s ...

  8. Udp发送

    string message = "0302"; byte[] sendbytes = Encoding.ASCII.GetBytes(message); remoteIpep = ...

  9. 初探JavaScript魅力(五)

    JS简易日历    innerHTML <title>无标题文档</title> <script> var neirong=['一','二','三','四','五' ...

  10. ubuntu apache 安装awstats 流量分析工具(命令方式)

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...