note 104136 added to language.namespaces
| From: | Anonymous at osu1 dot php dot net | Date: | Wed, 25 May 2011 18:06:14 +0000 |
| Subject: | note 104136 added to language.namespaces | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-180035@lists.php.net to get a copy of this message | ||
The keyword 'use' has two different applications, but the reserved word table links to
here.
It can apply to namespace constucts:
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"; ?>
Note that
felineCat::says()
should be
\feline\Cat::says()
(and similar for the others)
but this comment form deletes the backslash (why???)
The 'use' keyword also applies to closure constructs:
<?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);
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 184.39.6.84
----
Manual Page -- http://www.php.net/manual/en/language.namespaces.php
Edit -- https://master.php.net/note/edit/104136
Del: integrated -- https://master.php.net/note/delete/104136/integrated
Del: useless -- https://master.php.net/note/delete/104136/useless
Del: bad code -- https://master.php.net/note/delete/104136/bad+code
Del: spam -- https://master.php.net/note/delete/104136/spam
Del: non-english -- https://master.php.net/note/delete/104136/non-english
Del: in docs -- https://master.php.net/note/delete/104136/in+docs
Del: other reasons-- https://master.php.net/note/delete/104136
Reject -- https://master.php.net/note/reject/104136
Search -- https://master.php.net/manage/user-notes.php