PHP - 传递变量以包含文件的最佳实践 [英] PHP - Best Practice for Passing Variables to Include Files
问题描述
这更像是一个'如何应该我?'而不是'如何做我?'的问题。
This is more of a 'How should I?' rather than 'How do I?' question.
通常,什么被认为是将变量传递给包含文件的最佳方法?
Generally, what is considered the best way to pass variables to an included file?
例如,假设我正在使用片段作为菜单,并且想要一个菜单项(当前的一个)有一个特定的类名(这是一个非常通用的样本 - 而不是我实际使用的那个):
For example, let's say I'm using a fragment for a menu, and want one of the menu items (the current one) to have a certain class name (this is a very generic sample - not something I'm actually using):
<?php
$links = array(
array('text' => 'home', 'href' => 'home.php'),
array('text' => 'about', 'href' => 'about.php'),
array('text' => 'contact', 'href' => 'contact.php')
);
?>
<ul>
<?php for($i = 0; $i < 3; $i++) :
$link = $links[$i];
$is_active = ($i == $active_index);
?>
<li><a <?=($is_active ? 'class="active"' : '')?> href="<?=$link['href']?>"><?=$link['text']?></a></li>
<?php endfor; ?>
</ul>
我会调用上面的'menu.inc.php'。显然它正在寻找一个名为 $ active_index
的变量(int)来确定将'.active'类赋予哪个链接。
i'll call the above 'menu.inc.php'. obviously it's looking for a variable (int) called $active_index
to determine which link to give the '.active' class to.
所以 - 你可以在调用include之前定义 $ active_index
,但这对我来说似乎很糟糕,因为可能已经定义了该名称的变量之前的其他内容以及脚本的后续部分仍在寻找它。
so - you could just define $active_index
before calling the include, but this seems like poor practice to me since perhaps a variable of that name might have been defined for something else earlier and a later part of the script is still looking for it.
或 - 您可以使用绝对路径并使用查询字符串附加变量(包括'menu.inc.php?active_index = 1'
),但这似乎是一个坏主意,因为你可能需要'真实' $ _ GET
在任何给定的包含
。
or - you could use an absolute path and append variables using a querystring (include 'menu.inc.php?active_index=1'
), but again that seems like a bad idea since you might need the 'real' $_GET
within any given include
.
或 - 你可以用<$ c $开始每个包含的文件c> ob_start 并返回 ob_end_clean()
,然后使用类似的东西获得回报:
or - you could start each included file with ob_start
and return ob_end_clean()
, then use something like this to get the return:
function load_view($file, $variables){
extract($variables);
return include($file);
}
// passed like
<?=load_view('menu.inc.php', array('active_index' => 2))?>
但是这似乎有一些缺点(必须相应地重构所有的包含文件) ob
函数和 return
语句。)
but again this seems to have a number of cons (having to restructure all your include files accordingly with the ob
functions and a return
statement).
推荐答案
我喜欢这样的对象,如在这个MVC堆栈帖子中。在名为viewMenu.class.php的文件中,
I like an object for this, as described in this MVC stack post. In a file called viewMenu.class.php,
class viewMenu
{
private $active_link;
public function __construct ( $active_link )
{
$this->active_link = $active_link;
}
//If the constructor doesn't work for you, try a "set" method.
public function view ()
{
$active_link = $this->active_link;
include_once ( "menu.inc.php" );
}
}
在view方法中定义$ active_link包含变量范围在方法中的$ active_link。然后调用此代码:
Defining $active_link inside the view method contains the variable scope of $active_link to within the method. Then call this code:
$aViewMenu = new viewMenu( $active_link );
$aViewMenu->view();
但是,我几乎不熟悉PHP中的MVC,我欢迎责备。
However, I'm nearly new to MVC in PHP, and I welcome reproach.
这篇关于PHP - 传递变量以包含文件的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!