php定义命名空间,PHP 定义命名空间

用户评论:

wyattbiker (2013-06-01 00:19:45)

functionmyfunc(){

return1;

}

echomyfunc();?>

Here I can add stuff before the 2nd namespace

functionmyfunc(){

return2;

}

echomyfunc();?>

Switch back to first namespace

echomyfunc();?>

parsmizban.com (2012-06-12 11:02:39)

You can use this as a namespace declaration:

namespaceparsmizban;

echo"parsmizban";?>

huskyr at gmail dot com (2009-10-05 04:20:48)

"A file containing a namespace must declare the namespace at the top of the file before any other code"

It might be obvious, but this means that you *can* include comments and white spaces before the namespace keyword.

// of

// interesting

// comments and white spacenamespaceFoo;

classBar{

}?>

jeremeamia at gmail dot com (2009-07-14 08:43:55)

You should not try to create namespaces that use PHP keywords. These will cause parse errors.

Examples:

danbettles at yahoo dot co dot uk (2009-04-14 12:02:21)

Regarding constants defined with define() inside namespaces...

define() will define constants exactly as specified.  So, if you want to define a constant in a namespace, you will need to specify the namespace in your call to define(), even if you're calling define() from within a namespace.  The following examples will make it clear.

The following code will define the constant "MESSAGE" in the global namespace (i.e. "\MESSAGE").

The following code will define two constants in the "test" namespace.

David Drakard (2008-09-07 05:56:02)

I agree with SR, the new namespaces feature has solved a number of problems for me which would have required horrible coding to solve otherwise.

An example use:

Say you are making a small script, and write a class to connect to a database, calling it 'connection'. If you find your script useful and gradually expand it into a large application, you may want to rename the class. Without namespaces, you have to change the name and every reference to it (say in inheriting objects), possibly creating a load of bugs. With namespaces you can drop the related classes into a namespace with one line of code, and less chance of errors.

This is by no means one of the biggest problems namespaces solve; I would suggest reading about their advantages before citicising them. They provide an elegant solutions to several problems involved in creating complex systems.

Baptiste (2008-05-14 12:47:03)

There is nothing wrong with PHP namespaces, except that those 2 instructions give a false impression of package management.

... while they just correspond to the "with()" instruction of Javascript.

By contrast, a package is a namespace for its members, but it offers more (like deployment facilities), and a compiler knows exactly what classes are in a package, and where to find them.

Anonymous (2008-04-01 11:11:34)

@ RS: Also, you can specify how your __autoload() function looks for the files. That way another users namespace classes cannot overwrite yours unless they replace your file specifically.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值