来源: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. 好题 线段树对数据的保存+离线的逆向插入 POJ 2887

    题目大意:给一个字符串,有插入和询问操作,每次往一个位置插入一个字符或者询问第p个位置的字符是什么. 思路:我们离线询问,逆向把所有的字符都插入给线段树,然后再查询就好了,每次都要记得插入线段树的最后 ...

  2. CSS position 属性

    position: relative | absolute | static | fixed 参考网站:http://blog.csdn.net/dyllove98/article/details/8 ...

  3. 事务(JDBC、Hibernate、Spring)

    如果不用spring管理事务,我们自己写代码来操作事务.那么这个代码怎么写要看底层怎么访问数据库了. 当采用原生JDBC访问数据库时,操作事务需要使用java.sql.Connection的API.开 ...

  4. Swift 学习笔记(五)

    126. 协议(Protocols) 协议语法(Protocol Syntax) 属性要求(Property Requirements) 方法要求(Method Requirements) Mutat ...

  5. OpenCV4Android释疑: 透析Android以JNI调OpenCV的三种方式(让OpenCVManager永不困扰)

    OpenCV4Android释疑: 透析Android以JNI调OpenCV的三种方式(让OpenCVManager永不困扰) 前文曾详细探讨了关于OpenCV的使用,原本以为天下已太平.但不断有人反 ...

  6. 关于VC中的错误处理

    include <exception> try {} cache(exception &e) { cout<<e.what()<<endl; }     但 ...

  7. Android Studio 提示Error running app: No Android facet found for app

    错误解决办法如下: 可以通过以下几个步骤解决该问题: 1) 点击菜单File -> 选择Project Structure, 或使用快捷键 (Ctrl+Alt+Shift+S) 打开”Proje ...

  8. JavaBean--JavaBean与表单

    SimpleBean.java: package cn.mldn.lxh.demo ; public class SimpleBean { private String name ; private ...

  9. 使用float和display:block将内联元素转换成块元素的不同点

    使用float和display:block将内联元素转换成块元素的不同点 使用float和display:block将内联元素转换成块元素的不同点:内联元素可以转换成块级元素,常用的方法比如可以为内联 ...

  10. DIL中基本数据类型

    (1)基本数据类型:OMG IDL基本数据类型包括short.long和相应的无符号(unsigned)类型,表示的字长分别为16.32位.  (2)浮点数类型:OMG IDL浮点数类型包括float ...