關鍵字 'use' 有兩種不同的應用,但保留字表格卻連結到這裡。
它可以應用於命名空間結構
檔案 1
<?php namespace foo;
class Cat {
static function says() {echo 'meoow';} } ?>
檔案 2
<?php namespace bar;
class Dog {
static function says() {echo 'ruff';} } ?>
檔案 3
<?php namespace animate;
class Animal {
static function breathes() {echo 'air';} } ?>
檔案 4
<?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);
}
?>