$global = 'hello';
$bbb = function()use($global){
echo $global."\n";
};
$bbb();
//输出 'hello'
$global = 'hello';
$fun = function(){
global $global;
$global =' world';
echo $global."\n";
};
$fun();
// 输出 'world'
$global = 'hello';
$fun = function(){
global $global;
$global =' world';
echo 'a:'.$global."\n";
};
$ccc = function(){
echo 'b:'.$global;
};
$fun()
$ccc()
/*
a: world Notice: Undefined variable: global in xxxxxxx on line xx */
<?php
$global = 'hello';
$fun = function(){
global $global;
$global ='world';
echo 'a:'.$global."\n";
};
$bbb = function()use($global){
$global = 'china';
echo 'c:'.$global."\n";
};
$fun();
echo 'b:'.$global."\n";
$bbb();
echo 'd:'.$global;
a: world b: world c:china d: world
class Cart
{
const PRICE_BUTTER = 1.00;
const PRICE_MILK = 3.00;
const PRICE_EGGS = 6.95;
protected $products = array();
public function add($product, $quantity)
{
$this->products[$product] = $quantity;
}
public function getQuantity($product)
{
return isset($this->products[$product]) ? $this->products[$product] :
FALSE;
}
public function getTotal($tax)
{
$total = 0.00;
$callback =
function ($quantity, $product) use ($tax, &$total)
{
$pricePerItem = constant(__CLASS__ . "::PRICE_" .
strtoupper($product));
$total += ($pricePerItem * $quantity) * ($tax + 1.0);
};
array_walk($this->products, $callback);
return round($total, 2);
}
}
$my_cart = new Cart;
$my_cart->add('butter', 1);
$my_cart->add('milk', 3);
$my_cart->add('eggs', 6);
print $my_cart->getTotal(0.05) . "\n";
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有