关于 PHP 课程组织最佳方式的意见

发布于 2024-09-05 20:46:54 字数 943 浏览 4 评论 0原文

我几乎决定使用 Java 包命名约定

com.website.app.whatever

但我不确定在 PHP 中执行此操作的最佳方法。

选项 1:

创建目录结构

com/
    mysite/
          myapp/
               encryption/
                         PublicKeyGenerator.class.php
                         Cipher.class.php
               Xml/
                         XmlHelper.class.php
                         XmlResponse.class.php

Java 就是这样做的,它使用文件夹来组织层次结构。然而,这在 PHP 中有点笨拙,因为它没有本地支持,并且当您移动内容时,您会破坏所有包含。

选项 2

使用包的句点来命名类,因此名称就像 Java 中一样,但可以全部位于同一目录中,从而使 __autoload 变得轻而易举。

classes/
        com.mysite.myapp.encription.PublicKeyGenerator.class.php
        com.mysite.myapp.encription.Cipher.class.php
        com.mysite.myapp.xml.XmlHelper.class.php
        com.mysite.myapp.xml.XmlResponse.class.php

这里唯一真正的缺点是所有类都在一个文件夹中,这可能会让大型项目感到困惑。

征求意见,哪个是最好的,或者还有其他更好的选择吗?

I am pretty much set on using the Java package naming convention of

com.website.app.whatever

but am unsure about the best way of doing this in PHP.

Option 1:

Create a directory structure

com/
    mysite/
          myapp/
               encryption/
                         PublicKeyGenerator.class.php
                         Cipher.class.php
               Xml/
                         XmlHelper.class.php
                         XmlResponse.class.php

Now this is how Java does it, it uses the folders to organize the heirarchy. This is however a little clumsy in PHP because there is no native support for it and when you move things around you break all includes.

Option 2

Name classes using a periods for the package, therefore names are just like in Java but can all be in the same directory making __autoload a breeze.

classes/
        com.mysite.myapp.encription.PublicKeyGenerator.class.php
        com.mysite.myapp.encription.Cipher.class.php
        com.mysite.myapp.xml.XmlHelper.class.php
        com.mysite.myapp.xml.XmlResponse.class.php

The only real disadvantage here is that all the classes are in a single folder which might be confusing for large projects.

Opinions sought, which is the best or are there other even better options?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

涙—继续流 2024-09-12 20:47:02

我一直更喜欢带有较少文件夹的前缀文件,这样就可以减少导航次数,但这只是我个人的偏好。

如果您认为以后可能想要更改它,您可以创建一个中央包含脚本,例如:

<?php do_include('encryption_cypher'); ?>


   function do_include($file){
    if($file== 'encryption_cypher')
        include('class/app/someotherfolder/encryption/cypher.php');

    }

但是,从长远来看,这只是混乱,所以选择两害相权取其轻即可。

I've always preferred prefixed files with fewer folders so there is less navigating around, but it is just my personal preference.

if you think you might want to change it later, you can create a central include script like:

<?php do_include('encryption_cypher'); ?>


   function do_include($file){
    if($file== 'encryption_cypher')
        include('class/app/someotherfolder/encryption/cypher.php');

    }

However, this is just messy in the long-term, so pick the lesser of the two evils and go.

路还长,别太狂 2024-09-12 20:47:00

我建议使用选项 1,因为在这种布局中,代码是分离的,最终将成为一个更易于管理和灵活的系统。

I would suggest Option 1, because in that layout in particular is the separation of codes, which will end up as a much manageable and flexible system.

生生漫 2024-09-12 20:46:58

这不仅取决于您将有多少课程,还取决于您将拥有多少个网站和应用程序。如果您有多个网站或应用程序,那么使用文件夹会更合乎逻辑,这样您就不会感到困惑。如果您只有几个类(比如少于 20 个),那么将它们全部保存在一个文件夹中可能更合乎逻辑。根据上面的类类型以及您想要的详细程度,我会继续使用目录,这样您以后就不会看到它并说“天哪,我希望我使用了目录”。然后你最终会编写无用的程序,你只会使用这些程序一次,这样你就可以改变你的文件结构。

It depends not only on how many classes you're going to have, but also how many sites and apps you will have. If you have more than one site or app, it would be more logical to have folders so you don't get confused. If you only have a few classes (say less than 20), it might be more logical just to keep them all in one folder. Based on the kinds of classes above and how detailed you want to be, I'd go ahead and use directories so that later on you don't look at it and say "Gosh I wish I had used directories". Then you end up writing useless programs that you'll only ever use that one time just so you can change your file structure.

┈┾☆殇 2024-09-12 20:46:57

您可以遵循 Zend Framework 标准,例如

Option1,在 autoload 魔术回调中使用 autoload

new App_Encryption_Cipher

将 _ 替换为 '/' 并进行进一步检查(文件存在?正在查找的文件名中存在垃圾?符号?)

<块引用>

然而,这在 PHP 中有点笨拙,因为没有对它的本机支持,并且当 >>你移动周围的东西就打破了所有包含。

取决于您如何规划/设计您的应用程序。无论如何,在重构方面是无法逃脱的:)

我知道你已经习惯了java命名约定,但是如果你做了类似 (new com_mysite_myapp_encryption_cypher) 的事情,那么一直写起来会有点痛苦

You could follow Zend Framework standards like

Option1 with autoload

new App_Encryption_Cipher

in autoload magic callback replace the _ to '/' and do further checking (the file exists? garbage in the filename being seek? symbols?)

This is however a little clumsy in PHP because there is no native support for it and when >> you move things around you break all includes.

Depends on how you plan/design your application. there is no escape when it comes to refactoring anyway :)

I know you are used to java naming conventions, but if you do something like (new com_mysite_myapp_encryption_cypher) well, it kinda becomes a pain to write all the time

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文