类名中的文件路径约定怎么样?
你们(尤其是 PHP)对于类的命名约定(其中类名反映与项目目录相关的文件路径)有何看法? 例如:
# /project/Session/Abstract.php
# /project/Session/Database.php
class Session_Database extends Session_Abstract ...
我相信你明白了。 我也确信一些优点是显而易见的..但是您认为 - 延长类名以获得易于导航的漂亮目录结构是否值得?
这也允许在 PHP 中进行单行 __autoload( $class ) 定义: str_replace( '_', '/', $class );
我想有些人会认为这样的惯例是愚蠢的。 我个人很喜欢它,但我还没有看到其他人使用它,我不太确定它在实践中是否会很好用。
缺点之一可能是,随着 include/require 调用的删除,所有类都依赖于(粘合在一起)自动加载函数,有些人可能会认为这不符合他们对松散耦合的理解。
到目前为止,我所知道的有关这种方法的唯一参考是 http://softwareengineering.vazexqi。 com/files/pattern.html
那么,您对此有什么看法吗?
What do you (especially PHP) guys think about a naming convention for classes in which the class name reflects the path to the file, related to the project directory? e.g:
# /project/Session/Abstract.php
# /project/Session/Database.php
class Session_Database extends Session_Abstract ...
I'm sure you get the idea. I'm also sure some of the pros are obvious.. but what do you think - is it worth it to lengthen class names in order to get pretty nice directory structure which is easy to navigate?
This also allows for a one-liner __autoload( $class ) definition in PHP: str_replace( '_', '/', $class );
I suppose some people will consider such convention to be stupid. I personally like it, but I haven't seen it in use by other people and I am not quite sure if it will work so well in practice.
One of the cons might be that with the removal of include/require calls, all classes are dependent(glued together) on the autoload function, which some might argue, does not comply with their understanding of loose coupling.
The only reference that is known to me so far about such approach is http://softwareengineering.vazexqi.com/files/pattern.html
So, do you have an opinion on this one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
那你就没找过。 PEAR 使用这种方法。 Zend Framework 也是如此。 这是 PHP 世界中相当成熟的标准。 我不知道我什么时候第一次遇到它,但我个人已经使用它并且喜欢它好几年了,既作为我自己的框架的一部分,又作为 ZF 等公开可用框架的一部分。
Then you haven't been looking. PEAR uses this approach. So does Zend Framework. It's a rather established standard in the PHP world. I don't know when I first encountered it, but I've personally used it and liked it for several years, both as part of my own framework and as part of publicly available frameworks such as ZF.
我唯一不喜欢的一点是,当我需要移动代码时,我最终会重新处理很多名称。 然而,埃米尔已经指出,这几乎是课程的标准,但我想添加 PHP 用户空间命名规则。
The only real thing that I don't like about this is that when I need to move code around, I end up re-working lots of names. However, Emil has already pointed out that this is pretty much par for the course, but I'd like to add the PHP Userland Naming Rules.
我也使用这个约定。
它不仅有助于拥有漂亮的文件结构,还有助于解决命名空间问题。 该命名约定有助于为类添加前缀以避免重复的类名。
当 PHP 5.3 发布并最终拥有名称空间时,我仍将使用此约定。 我唯一要做的不同就是缩短我的类名,将我的前缀转换为正确的命名空间,并在我的自动加载器中进行调整。
I use that convention aswell.
Not only does it help have a pretty file structure, it also helps with namespacing issues. That naming convention helps prefixing classes to avoid duplicate class names.
When PHP 5.3 comes out and finally has namespaces, i'll still be using this convention. The only thing I'll do differently is shorten my class names, convert my prefixes into proper namespaces and do adjustment in my autoloader.