Comments (5)
在PHP中,匿名函数的作用,大部分作用是作为一次性函数,回调,很少作为向JavaScript那样使用闭包
$fn = function(){};
$fn();
array_map(function(){},$array);
javascript中 为了保存内部变量
var f = function(){
$a=1
//产生闭包
return function(){
return $a++
}
}
f1 = f()
f1()//1
f1()//2
from php-interview.
嗯嗯,对的,js中闭包是能访问到外部变量的,而PHP需要use;我的意思是在那句话前加个(在PHP中),会不会比较严谨点?哈哈哈我就无聊随便说说:)
from php-interview.
没事! @bestcyt 相互交流而已。 PHP的匿名函数 一般就是作回调。Js的匿名函数只要是为了保存一些内部变量,产生闭包。 js的作用域和PHP的也不太一样,所以两个语言相互理解一下
$a = 10;//外部变量 非全局
function f () {
echo $a; //内部的$a 未定义
}
# use 将a加到函数的作用域下
$f1 = function () use ($a) {
echo $a;
};
$f1();
var a = 10 //全局变量
function f(){
alert(a)//10
}
from php-interview.
@xianyunyh 大佬这边子类应该是不能用parent来获取父类的保护属性,要用this,你看看是不是酱紫?
from php-interview.
@bestcyt 可能我写错了! 以下是用法和官方的介绍。
感谢指正
范围解析操作符(也可称作 Paamayim Nekudotayim)或者更简单地说是一对冒号
::
,可以用于访问 静态成员,类常量,还可以用于覆盖类中的属性和方法。
class A
{
const PI = 3.14;
public static $static_var = "A的静态变量";
public function __construct()
{
echo "this is A" . "\n";
}
}
class B extends A
{
public function __construct()
{
parent::__construct();
}
public function test()
{
echo parent::PI . "\n";
echo parent::$static_var;
}
}
(new B())->test();
参考链接
from php-interview.
Related Issues (8)
- 错的地方太多了 HOT 1
- 建议整理成Gitbook格式 HOT 2
- readme.md很多目录目录的链接都失效了 HOT 1
- 在Linux目录下的进程和线程的链接 链 到了shell HOT 3
- 这边有图嘛? HOT 4
- PHP-Interview/面试/笔试题4.md 的第三条输出结果错误 HOT 1
- 笔误??? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from php-interview.