php 语法中有 let 吗?
来源: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 吗?的更多相关文章
- 简体中国版文档的Markdown语法
Markdown文件 注意︰这是简体中国版文档的Markdown语法.如果你正在寻找英语版文档.请参阅Markdown︰ Markdown: Syntax. Markdown: Syntax 概述 哲 ...
- Markdown语法说明(详解版)
####date: 2016-05-26 20:38:58 tags: Markdown tags && Syntax ##Markdown语法说明(详解版)杨帆发表于 2011-11 ...
- Markdown语法手册
Markdown 语法手册 Markdown 是一种轻量级标记语言,能将文本换成有效的XHTML(或者HTML)文档,它的目标是实现易读易写,成为一种适用于网络的书写语言. Markdown 语法简洁 ...
- Markdown 语法说明
Markdown 语法说明 (简体中文版) / (点击查看快速入门) 概述 宗旨 Markdown 的目标是实现「易读易写」. 可读性,无论如何,都是最重要的.一份使用 Markdown 格式撰写的文 ...
- [开发笔记]-MarkDown语法
马克飞象MarkDown在线编辑器 http://maxiang.info/?basic 1. H1--H6 相应数量的# 2. 底线形式 = H1(最高阶标题)和- H2(第二阶标题) 3. 段落和 ...
- markdown语法集锦
参考:http://wowubuntu.com/markdown/#blockquote 1. 标题 # 一级标题 ## 二级标题 ### 三级标题 共六级标题 2. 列表 有序列表:1,2,3: 无 ...
- Markdown 语法说明 (简体中文版)
http://wowubuntu.com/markdown/#editor 概述 宗旨 兼容 HTML 特殊字符自动转换 区块元素 段落和换行 标题 区块引用 列表 代码区块 分隔线 区段元素 链接 ...
- [转]Markdown 语法手册
Markdown 是一种轻量级标记语言,能将文本换成有效的XHTML(或者HTML)文档,它的目标是实现易读易写,成为一种适用于网络的书写语言. Markdown 语法简洁明了,易于掌握,所以用它来写 ...
- .md文件 Markdown 语法说明
Markdown 语法说明 (简体中文版) / (点击查看快速入门) 概述 宗旨 兼容 HTML 特殊字符自动转换 区块元素 段落和换行 标题 区块引用 列表 代码区块 分隔线 区段元素 链接 强调 ...
随机推荐
- Linux 终端下颜色的输出
在命令行下也能产生五颜六色的字体和图案,只需要加上一些颜色代码,例如 echo -e "\033[41;36m 红底绿字\033[0m" 其中41的位置代表底色, 36的位置是代表 ...
- php添加gd
一 GD简介: php处理图形的扩展库,提供了一系列用来处理图片的API.如果开发过程中发现有页面验证码不能显示,则要考虑检查phpinfo(),是否支持GD库. 二 思路: 网上发现添加GD库的方法 ...
- C#操作SQLite 报错 (Attempt to write a read-only database)
解决办法:找到SQLite数据库所在的文件夹,单击右键,属性->安全,为Users用户组添加写入权限.
- cakephp2.3.0 lib中的Model.php有一个bug
1. cakephp2.3.0 lib中的Model.php有一个bug, 加上 !empty($db->config['prefix']) 这个判断更好.有时候会少进行一次 new PDO() ...
- get 和 post请求的区别
(1)GET请求用于获取信息,从Client的角度看,不会改变资源状态,并且多次对同一URL的多个请求应该返回相同的结果. GET请求的参数会显示在URL中,即放置在HTTP协议头中(所 ...
- C++函数后面的throw()
看CImage函数实现的时候发现了这么个东东 inline HBITMAP CImage::Detach() throw() 它是函数提供者和使用者的一种君子协定,标明该函数不抛出任何异常. 之所以 ...
- java数据结构之二叉树的实现
java二叉树的简单实现,可以简单实现深度为n的二叉树的建立,二叉树的前序遍历,中序遍历,后序遍历输出. /** *数据结构之树的实现 *2016/4/29 * **/ package cn.Link ...
- python 第三方库下载
C:\Python27\Scripts 路径下: easy_install.exe: C:\Python27\Scripts>easy_install.exe pycrypto pip.exe: ...
- JS-DOM操作应用高级(三)
appendChild 1.先把元素从原有的父级上删除 2.添加到新的父级 <title>无标题文档</title> <script> window.on ...
- html标签中head中两个标签的作用
<meta name="render" content="webkit"> //浏览器使用急速模式打开 <meta http-equi ...