php exeil导入,使用命名空间:别名/导入

用户评论:

[#1]

sernuzh at gmail dot com [2015-05-23 07:18:47]

You'll get here the

Fatal error: Cannot declare class others\name because the name is already in use

So you can't get two classes  inside one namespace

classname{

public function__construct(){

echo'my_namespace_object';

}

}

}

namespaceothers{

usemy\name;

classname{

public function__construct(){

echo'others_namespace_object';

}

}$newObject= newname();

}?>

[#2]

kelerest123 at gmail dot com [2015-03-10 17:50:34]

For the fifth example (example #5):

When in block scope, it is not an illegal use of use keyword, because it is used for sharing things with traits.

[#3]

Dr. Gianluigi "Zane" Zanettini [2015-02-13 13:33:56]

I was attempting to use something like this:

?>

This is not supported. I did this instead:

useMy\First\Namespace;

else

useMy\Other\Namespace;?>

[#4]

anon [2014-01-30 21:08:48]

The <?phpuse ?> statement does not load the class file. You have to do this with the <?phprequire ?> statement or by using an autoload function.

[#5]

k at webnfo dot com [2013-04-07 13:32:55]

Note that you can not alias global namespace:

use \ as test;

echo test\strlen('');

won't work.

[#6]

Anonymous [2013-04-03 10:02:25]

The last example on this page shows a possibly incorrect attempt of aliasing, but it is totally correct to import a trait \Languages\Languages\Danish.

[#7]

cl [2013-03-11 00:59:11]

Something that is not immediately obvious, particular with PHP 5.3, is that namespace resolutions within an import are not resolved recursively.  i.e.: if you alias an import and then use that alias in another import then this latter import will not be fully resolved with the former import.

For example:

use \Controllers as C;

use C\First;

use C\Last;

Both the First and Last namespaces are NOT resolved as \Controllers\First or \Controllers\Last as one might intend.

[#8]

x at d dot a dot r dot k dot REMOVEDOTSANDTHIS dot gray dot org [2013-01-02 05:13:13]

You are allowed to "use" the same resource multiple times as long as it is imported under a different alias at each invocation.

For example:

useLend\l1;

useLend\l1asl3;

useLend\l2;

useLend\l1\Keller;

useLend\l1\KellerasStellar;

useLend\l1\KellerasZellar;

useLend\l2\KellerasDellar;

...?>

In the above example, "Keller", "Stellar", and "Zellar" are all references to "\Lend\l1\Keller", as are "Lend\l1\Keller", "l1\Keller", and "l3\Keller".

[#9]

samuel dot roze at gmail dot com [2012-05-11 13:34:40]

(All the backslashes in namespaces are slashes because I can't figure out how to post backslashes here.)

You can have the same "use" for a class and a namespace. For example, if you have these files:

classbar{

public function__toString()

{

return'foo\bar\__toString()';

}

}?>

classMyClass{

public function__toString()

{

return'foo\bar\MyClass\__toString()';

}

}?>

In another namespace, you can do:

require_once'foo/bar.php';

require_once'foo/bar/MyClass.php';

usefoo/bar;$bar= newbar();

echo$bar."\n";$class= newbar/MyClass();

echo$class."\n";?>

And it will makes the following output:

foo\bar\__toString()

foo\bar\MyClass\__toString()

[#10]

c dot 1 at smithies dot org [2011-08-14 15:26:15]

If you are testing your code at the CLI, note that namespace aliases do not work!

(Before I go on, all the backslashes in this example are changed to percent signs because I cannot get sensible results to display in the posting preview otherwise. Please mentally translate all percent signs henceforth as backslashes.)

Suppose you have a class you want to test in myclass.php:

classmyclass{// ...}?>

and you then go into the CLI to test it. You would like to think that this would work, as you type it line by line:

require 'myclass.php';

use my%space%myclass; // should set 'myclass' as alias for 'my%space%myclass'

$x = new myclass; // FATAL ERROR

I believe that this is because aliases are only resolved at compile time, whereas the CLI simply evaluates statements; so use statements are ineffective in the CLI.

If you put your test code into test.php:

usemy%space%myclass;$x= newmyclass;//...?>

it will work fine.

I hope this reduces the number of prematurely bald people.

[#11]

Jan Tvrdk [2011-01-11 06:26:49]

Importing and aliasing an interface name is also supported.

[#12]

thinice at gmail.com [2010-12-01 22:07:28]

Because imports happen at compile time, there's no polymorphism potential by embedding the use keyword in a conditonal.

e.g.:

useAnimal\CanineasBeast;

}

if ($objType=='bovine') {

useAnimal\BovineasBeast;

}$oBeast= newBeast;$oBeast->feed();?>

[#13]

nsdhami at live dot jp [2010-07-15 02:01:17]

The "use" keyword can not be declared inside the function or method. It should be declared as global, after the "namespace" as:

functionfun1()

{// Parse error: syntax error, unexpected T_USEusemydir/subdir/Class1asClass1;

}

classClass2{

public functionfun2()

{// Parse error: syntax error, unexpected T_USEusemydir/subdir/Class1asClass1;

}

}?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值