略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: 提示

2024-03-28

提示

要写出能经受住时间考验的代码,建议在全局的命名空间中,尽量不要用变量、函数或类名,以避免与其它用户空间代码出现命名空间冲突。

防止函数和类的命名冲突的一个常见方法是使用自己专属名字的 namespace

<?php

namespace MyProject;

function 
my_function() {
    return 
true;
}

\
MyProject\my_function();

这仍然需要你跟踪已经使用过的命名空间,但一旦你用了在决定了要使用的命名空间后,你可以添加所有的函数和类到它,不用再考虑冲突。

最好的做法是,尽量控制一下添加到全局范围内的变量,以防止与第三方代码的命名产生命名空间冲突。

注意: Variable scoping

Because of PHP's scoping rules variables defined inside functions and methods are not in the global scope and as such cannot conflict with other variables defined in the global scope.

add a noteadd a note

User Contributed Notes

There are no user contributed notes for this page.

官方地址:https://www.php.net/manual/en/userlandnaming.tips.php

冷却塔厂家 广告
-- 广告
北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3