欢迎各位兄弟 发布技术文章
这里的技术是共享的
$func = function(){
exit('Hello world!!');
};//这里必须要有;结尾
$func();
复制代码
function func(){
exit('Hello world!!');
}
func();
复制代码
function operate($operator){
if($operator == "-"){
return function($a,$b){
return $a-$b;
};
}else{
return function($a,$b){
return $a+$b;
};
}
}
$subtraction = operate("-");
echo $subtraction(4,3);//1
$addition = operate("+");
echo $addition(1,2);//3
复制代码
<?php
header("Content-Type:text/html;charset=utf-8");
class Di{
private $_factory;
public function set($id,$value){
$this->_factory[$id] = $value;
}
public function get($id){
$value = $this->_factory[$id];
return $value();
}
}
class User{
private $_username;
function __construct($username="") {
$this->_username = $username;
}
function getUserName(){
return $this->_username;
}
}
//从这里开始看
$di = new Di();
$di->set("zhangsan",function(){
return new User('张三');
});
$di->set("lisi",function(){
return new User("李四");
});
echo $di->get("zhangsan")->getUserName();
echo $di->get("lisi")->getUserName();
复制代码
function func1($a){
return function() use ($a){
echo $a;
};
}
$a = func1("a");
$a();//输出a
复制代码