PHP函数包含文件的设计技巧(Design Tips for PHP Function Include Files)

编程入门 行业动态 更新时间:2024-10-10 17:33:15
PHP函数包含文件的设计技巧(Design Tips for PHP Function Include Files)

好的设计决定了每个函数只写一次。 在PHP中,我使用包含文件(如Utils.php和Authenticate.php)和PHP命令include_once来完成此操作。 但是我一直无法找到任何PHP包含文件的标准或最佳实践。 你会在StackOverflow中建议什么?

我在找:

命名标准 代码标准 设计模式 建议定义常用函数的返回类型(现在我只使用关联数组)。

Good design dictates only writing each function once. In PHP I'm doing this by using include files (like Utils.php and Authenticate.php), with the PHP command include_once. However I haven't been able to find any standards or best practices for PHP include files. What would you at StackOverflow suggest?

I'm looking for:

Naming Standards Code Standards Design Patterns Suggestions for defining return types of common functions (now I'm just using associative arrays).

最满意答案

我喜欢使用的一种约定是将每个类放在自己的名为ClassName.class.php的文件中,然后设置自动加载器以包含类文件。 或者有时我会将它们全部放在classes /子目录中,并将它们命名为ClassName.php。 取决于我期待的课程与非课程的数量。

如果您将实用函数组织到类中并改为静态方法,那么您可以在顶级文件中只写一个require_once() 。 这种方法可能适用于您的代码或编码风格,也可能不适用。

至于返回类型,我尝试遵循内置函数中使用的约定。 返回适合请求的类型,或者在失败时返回false。 在结果中检查false时,请确保使用===运算符 。

您关注惯例这一事实表明您已经走上正轨。 如果您熟悉任何其他OOP语言,如Java,C ++,C#等,那么您会发现由于PHP5中的OOP优点,您可以遵循许多相同的约定。

One convention I like to use is to put each class in its own file named ClassName.class.php and then set up the autoloader to include the class files. Or sometimes I'll put them all in a classes/ subdirectory and just name them ClassName.php. Depends on how many class vs. non-class includes I'm expecting.

If you organize your utility functions into classes and make them static methods instead, you can get away with writing only a single require_once() in your top level files. This approach may or may not be appropriate for your code or coding style.

As for return types, I try to follow the conventions used in the built-in functions. Return a type appropriate to the request, or return false on failure. Just make sure you use the === operator when checking for false in the results.

The fact that you're concerned about conventions suggests you're already on the right track. If you are familiar with any other OOP language like Java, C++, C#, etc., then you'll find you can follow a lot of the same conventions thanks to the OOP goodness in PHP5.

更多推荐

PHP,I'm,StackOverflow,使用,标准,电脑培训,计算机培训,IT培训"/> <meta name=&q

本文发布于:2023-04-29 04:16:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1334441.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   技巧   文件   PHP   Design

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!