【发布时间】:2018-07-02 00:21:28
【问题描述】:
我正在开发一个框架,以使我的工作流程更快,例如入门主题和样板。所以使工作更快的主要部分之一是复数......
例如,当我需要创建新的帖子类型而不是为每个新的自定义帖子类型编写一大块代码(130 行)时,我在 function.php 中...我将代码移动到单独的文件夹然后我调用它由
require_once( 'library/custom-post-type.php' );
通过这种方式,我可以将许多现成的自定义帖子类型作为文件,并且对于每个项目,我也可以添加文件以供将来维护,而不是在非常大的 1000 多个函数文件中滚动...这将是一个包含 require_once 列表和文件夹的短文件,其中所有代码都在单独的文件中
我的问题:
- require_once 吗?影响网站速度?
- 什么时候应该使用它,什么时候不应该使用它
- 过度使用它以获得更有条理的代码有什么害处?
这个问题更像是 PHP / 服务器相关而不是 WordPress。
编辑 1: 例如,假设我有这个文件
案例 1
1 个文件
function.php
// lots of code
// 130 line of code to create custom post 1
// 130 line of code to create custom post 2
// 130 line of code to create custom post 3
// 130 line of code to create custom post 4
// 130 line of code to create custom post 5
// lots of code
案例 2
代码量相同的6个文件+5行require_once
function.php
// lots of code
require_once( 'library/custom-post-type1.php' );
require_once( 'library/custom-post-type2.php' );
require_once( 'library/custom-post-type3.php' );
require_once( 'library/custom-post-type4.php' );
require_once( 'library/custom-post-type5.php' );
// lots of code
custom-post-type1.php
// 130 line of code to create custom post 1
custom-post-type2.php
// 130 line of code to create custom post 2
custom-post-type3.php
// 130 line of code to create custom post 3
custom-post-type4.php
// 130 line of code to create custom post 4
custom-post-type5.php
// 130 line of code to create custom post 5
为什么我更喜欢这种情况,因为较小的文件易于查找和编辑,并且可以将任何文件移动到其他项目并调用它
我想创建一些模块结构...但不能损害速度
【问题讨论】:
-
您不应该在 PHP
include或require的上下文中考虑 IO。如果您正在接近 IO 真正重要的负载,则包含将位于瓶颈列表的底部。恕我直言,你应该着眼于语言的 OOP 特性,如类、命名空间、包和作曲家,专门编写更多......现代和模块化的代码。
标签: php mysql wordpress performance pagespeed