PHP + WordPress 函数组织:function() 和 Include() 哪个更好?

发布于 2025-01-04 00:27:46 字数 1628 浏览 4 评论 0原文

这个问题有两个部分:

#1

我有一个functions.php,其中填充有函数。我必须滚动才能找到我想要的功能,即使有注释来描述一个功能的结束位置和下一个功能的开始位置,有时也很难找到我想要的功能。

我已将函数分解为单独的 PHP 文件,并使用例如 include_once 'assets/functions/author_list.php'; 来使functions.php 文件更加清晰。

所以第一个问题是:在functions.php中使用多个includes会减慢加载时间或影响前端或后端的网站性能吗?

#2

第二个问题类似。如果我在函数中有如下函数。

<?php
function ao_post_meta() {
?>
                        <div class="post-meta">                     
                            <time datetime="<?php the_time('Y-m-d'); ?>"><?php the_time('M j, Y'); ?></time>
<?php if('videos' == get_post_type(get_the_ID())) { ?>
                            <a class="author-link" href="<?php bloginfo('wpurl'); ?>/character/<?php echo get_the_author_meta( 'user_nicename' ); ?>">
                                <span><?php the_author(); ?></span>
                            </a>
<?php } else { ?>
                            <a class="author-link" href="<?php echo get_author_posts_url(get_the_author_meta( 'ID' )); ?>">
                                <span><?php the_author(); ?></span>
                            </a>
<?php }?>
                            <a class="comment-link" href=""><?php echo get_comments_number(); ?></a> 
                        </div>
<?php
}
?>

是否最好简单地将其放在单独的 PHP 文件 my-post-meta.php 中,并在模板中使用 include(my-post-meta.php) 而不是将其作为函数调用?

谢谢!

This question has two parts:

#1

I have a functions.php that is filled with functions. I have to scroll to find the function I want, and even with comments to delineate where one ends and the next begins, it's sometimes difficult to find what I want.

I have broken up the functions into individual PHP files and used, for example, include_once 'assets/functions/author_list.php'; to make the functions.php file much cleaner.

So the first question is: will using multiple includes in functions.php slow down load time or affect site performance on the front or back end?

#2

Second question is similar. If I have a function such as the following in functions.

<?php
function ao_post_meta() {
?>
                        <div class="post-meta">                     
                            <time datetime="<?php the_time('Y-m-d'); ?>"><?php the_time('M j, Y'); ?></time>
<?php if('videos' == get_post_type(get_the_ID())) { ?>
                            <a class="author-link" href="<?php bloginfo('wpurl'); ?>/character/<?php echo get_the_author_meta( 'user_nicename' ); ?>">
                                <span><?php the_author(); ?></span>
                            </a>
<?php } else { ?>
                            <a class="author-link" href="<?php echo get_author_posts_url(get_the_author_meta( 'ID' )); ?>">
                                <span><?php the_author(); ?></span>
                            </a>
<?php }?>
                            <a class="comment-link" href=""><?php echo get_comments_number(); ?></a> 
                        </div>
<?php
}
?>

Is it better to simply put that in a separate PHP file, my-post-meta.php, and use include(my-post-meta.php) in my template rather than calling it as a function?

Thanks!

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

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

发布评论

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

评论(1

丿*梦醉红颜 2025-01-11 00:27:46

#1
是的,会的。但我建议你不用担心,因为:

  • 如果你的包含文件量不大,影响很小,根本不可见;
  • 托管处有针对PHP的操作码缓存扩展,可以减少include调用的影响;

#2
我更喜欢将所有模板提取到单独的 phtml 文件中,这些文件存储在单独的文件夹中(例如“模板”),并在需要时包含它。它将提高函数的可读性。例如,您将有以下代码:

<?php
function ao_post_meta() {
    include './templates/mycustom-template.phtml';
}
?>

一般来说,我的建议如下:

尝试保持良好/直观的组织文件夹/文件结构,而不是
将所有内容放入一个文件中。它将促进发展和
维护更容易。

#1
Yes, it will. But I recommend you don't worry about it, because:

  • If your amount of included files is not big it will have really small influence, which won't be visible at all;
  • There is opcode cache extension for PHP at the hosting, and it will reduce influence of include calls;

#2
I prefer to extract all templates into separate phtml files which are stored in separate folder (for example "templates") and include it when it's needed. It will increase readability of your functions. For example you will have following code:

<?php
function ao_post_meta() {
    include './templates/mycustom-template.phtml';
}
?>

In general my advise is following:

Try to keep nice/intuitive organized folder/files structure instead of
putting everything in one file. It will make development and
maintaining easier.

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