doulu1968 2012-02-10 15:04
浏览 29
已采纳

PHP + Wordpress功能组织:哪个更好,function()还是Include()?

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!

  • 写回答

1条回答 默认 最新

  • doz59484 2012-02-10 15:14
    关注

    #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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)