【问题标题】:Function Header vs Include Header php? - Upsides and Downsides函数标题与包含标题 php? - 优点和缺点
【发布时间】:2015-01-29 05:28:16
【问题描述】:

我主要寻找性能、网站缓存、易于打字和安全性 - 优点和缺点

拆分文件并将它们作为包含项放入其中,这似乎是一种共享偏好

<?php
include('header.php');//and whatever includes inside 
include('footer.php');//and whatever includes inside 
?>

VS

将它们制成函数并在一个文件中调用它们,您可以在其中检查值并在两个函数之间共享它们,例如多样式选择

<?php
include $_SERVER['DOCUMENT_ROOT'] . '/more-functions.php';
if(isset($_cookie['style']) || isset($_session['style'])){
    if(isset($_cookie['style'])){$style = $_cookie['style']}else{$style = $_session['style']}
}
function siteheader($style){
    if(!isset($style)){$style = 'default';}
    print ('<http>');//insert rest of header
}
function sitefooter($style){
    if(!isset($style)){$style = 'default';}
    print ('</http>');//insert rest of footer
}
?>

【问题讨论】:

  • 性能:可能存在细微差别,但没有什么实质性的差异,以至于您可以检测到它。另外:对其进行基准测试。缓存:没有任何影响。易于打字:自己决定。安全: ??? “安全”是什么?
  • 附带说明,当变量为小写时,您无法访问 cookie/会话。它必须是$_COOKIE/$_SESSION

标签: php performance caching


【解决方案1】:
include('header.php');//and whatever includes inside  
include('footer.php');//and whatever includes inside 

这打破了PSR-1

虽然几乎不可避免地使用页眉和页脚,无论是显式调用还是隐式调用,都会导致 HTML 片段格式错误,但当您显式调用它(作为函数/方法)时,您至少可以在内部定义两个片段同一个文件。

性能不会有显着差异。

【讨论】:

    猜你喜欢
    • 2011-10-10
    • 1970-01-01
    • 2011-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    相关资源
    最近更新 更多