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 global
keyword 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 特殊字符自动转换 区块元素 段落和换行 标题 区块引用 列表 代码区块 分隔线 区段元素 链接 强调 ...
随机推荐
- make module失败的原因cc1: error: unrecognized command line option “-m64
cc1: error: unrecognized command line option "-m64"cc1: error: unrecognized command line o ...
- vi命令的常用操作
G:移动到底部 进入vi的命令 vi filename :打开或新建文件,并将光标置于第一行首 vi +n filename :打开文件,并将光标置于第n行首 vi + filename :打开文件, ...
- layer属性
键: 值 描述 下表的属性都是默认值,您可在调用时按需重新配置,他们可帮助你实现各式各样的风格.如是调用: $.layer({键: 值, 键: 值, -}); type: 0 层的类型.0:信息框(默 ...
- Count on the path
Count on the path Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- awstats + tomcat + windows
下载: 1.apache-tomcat-7.0.67 2.ActivePerl-5.22.1.2201-MSWin32-x86-64int-299574.msi 3.awstats-7.4.zip 修 ...
- sqlserver 2008 查看表描述,和表结构
sp_help sys_user sp_columns sys_user --表结构 THEN obj.name ELSE '' END AS 表名, col.colorder AS 序号 , c ...
- OpenCV4Android释疑: 透析Android以JNI调OpenCV的三种方式(让OpenCVManager永不困扰)
OpenCV4Android释疑: 透析Android以JNI调OpenCV的三种方式(让OpenCVManager永不困扰) 前文曾详细探讨了关于OpenCV的使用,原本以为天下已太平.但不断有人反 ...
- apache-tomcat-7.0.70无法进入Manager管理App项目
在tomcat文件夹找到conf文件夹中的tomcat-user.xml文件,用记事本打开,在最下面可以看到tomcat默认把用户注释掉了,也就是说打开tomcat主页是进不去管理页面的.方法如下:找 ...
- Ubuntu + Django + Nginx + uwsgi
环境 Ubuntu 14.04 Python 2.7 Django 1.8.4 1 安装Nginx sudo apt-get install nginx 测试 sudo /etc/init. ...
- Datatable.select() 方法的使用
文章为转载 ,原文地址 DataTable是我们在进行开发时经常用到的一个类,并且经常需要对DataTable中的数据进行筛选等操作,下面就介绍一下Datatable中经常用到的一个方法--Selec ...