©
Dieses Dokument verwendet PHP-Handbuch für chinesische Websites Freigeben
[#1] fjalfajlfjl at xyz dot com [2015-11-01 12:54:54]
afalfalfjl
flafjalfjlafjlafjlasfj
jflaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
lfjlaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
sagljalgjslgjwiewiejiwojtow
[#2] davidkennedy85 at gmail dot com [2015-03-26 17:04:50]
In addition to using namespaces and closures, the use keyword has another new meaning as of PHP 5.4 - using traits:
<?php
trait Hello {
public function sayHello() {
echo 'Hello ';
}
}
trait World {
public function sayWorld() {
echo 'World';
}
}
class MyHelloWorld {
use Hello, World;
public function sayExclamationMark() {
echo '!';
}
}
$o = new MyHelloWorld();
$o->sayHello();
$o->sayWorld();
$o->sayExclamationMark();
?>
More info here: http://php.net/manual/en/language.oop5.traits.php
[#3] Ronald W [2015-01-22 13:13:37]
<?php
// multiple namespaces in a single file:
namespace foo{
class Cat {
static function says() {echo 'meoow';} }
}
namespace bar{
class Dog {
static function says() {echo 'ruff';} }
class Cat {
static function says() {echo 'meowi';} }
class Mouse { //nonstatic function
function says() {echo 'Come and get me ;)';} }
}
namespace animate{
class Animal {
static function breathes() {echo 'air';} }
}
namespace{ // No Namespace: global code
use foo as feline;
use bar as canine;
use bar\Mouse as MouseOnly;
use animate;
echo animate\Animal::breathes(), "<br />\n";
echo feline\Cat::says(), "<br />\n"; //not starting with a slash!
echo canine\Cat::says(), "<br />\n";
echo canine\Dog::says(), "<br />\n";
//any of the three following lines work:
// $micky=new bar\Mouse();
// $micky=new canine\Mouse();
$micky=new test();
echo $micky->says();
}
?>
[#4] php at lanar dot com dot au [2014-02-03 05:16:07]
Be warned that it is not trivial to convert a project to use namespaces. If you add the same namespace to the top of all your files, your application will break.
All objects in the global namespace, such as ArrayObject, must have a backslash (\) prepend to them.
If you use get_class() and instanceof, you will have to make adjustments here to.
If you have an autoloader, you will need to change its behavior.
It can be done, but subtle errors can creep in and are tedious to rectify.
[#5] Anonymous [2011-05-25 11:06:14]
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);
}
?>