关键字“use”有两个不同的应用,但保留字表链接到此处。
它可以应用于命名空间构造
file1
<?php namespace foo;
class Cat {
static function says() {echo 'meoow';} } ?>
file2
<?php namespace bar;
class Dog {
static function says() {echo 'ruff';} } ?>
file3
<?php namespace animate;
class Animal {
static function breathes() {echo 'air';} } ?>
file4
<?php namespace fub;
include 'file1.php';
include 'file2.php';
include 'file3.php';
use foo as feline;
use bar as canine;
use animate;
echo \feline\Cat::says(), "<br />\n";
echo \canine\Dog::says(), "<br />\n";
echo \animate\Animal::breathes(), "<br />\n"; ?>
请注意
felineCat::says()
应该是
\feline\Cat::says()
(以及其他类似情况)
但此评论表单删除了反斜杠(为什么???)
“use”关键字也应用于闭包构造
<?php function getTotal($products_costs, $tax)
{
$total = 0.00;
$callback =
function ($pricePerItem) use ($tax, &$total)
{
$total += $pricePerItem * ($tax + 1.0);
};
array_walk($products_costs, $callback);
return round($total, 2);
}
?>