PHP 5.3 编码标准:模板中的匿名函数
我似乎找不到任何文档说明在使用 PHP 模板化 HTML 时模板中的匿名函数是否是一个好主意。例如,我有以下代码:
<html><body>
<?
$listMethod = function($items)
{
?>
<ul>
<?foreach ($items as $item):?>
<li><?=$item?></li>
<?endforeach;?>
</ul>
<?
};
?>
<?=$listMethod(array('1','2','3'))?>
<p> AND </p>
<?=$listMethod(array('a','b','c'))?>
</body></html>
这是在 PHP 中创建模板的好方法还是坏方法?
I can not seem to find any documents stating if anonymous functions in templates are a good idea when templating HTML with PHP. I have the following code for example:
<html><body>
<?
$listMethod = function($items)
{
?>
<ul>
<?foreach ($items as $item):?>
<li><?=$item?></li>
<?endforeach;?>
</ul>
<?
};
?>
<?=$listMethod(array('1','2','3'))?>
<p> AND </p>
<?=$listMethod(array('a','b','c'))?>
</body></html>
Is this a good or bad way to create templates in PHP?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
糟糕的方式,调试它们、编辑、查找......将非常困难。您可以在模板顶部包含一个 template_functions.php 文件,并在那里存储所有与模板相关的函数/帮助程序。
编辑
如果您符合条件标准,请不要使用短标签,大多数托管公司都会允许它们(短标签),但有一些公司不会,所以您会遇到问题。
Bad bad way , it will be realy hard to debug them, edit, find, ... . You could include a template_functions.php file at the top of you're template and store all template related functions/helpers there .
Edit
Allso do not use short tags if you're into conding standards , most hosting companyes will allow them ( short tags ) but a few whont so you'll have problems .
将视图视为仅用于输出的东西,仅此而已。
除了 echo、if 和 循环 之外的所有内容都应该引起您的关注。 (尤其函数定义)
Look at views as something that's just for output, nothing else.
Everything except echo, if and loops should concern you if in there. (Especially function definitions)
这取决于您是否打算在多个视图中重复使用该功能。如果这样做,您可能需要给该函数一个名称,并将其放在某个地方,以便多个视图可以使用它。如果您不想再次使用该函数,可以使用匿名函数。话又说回来,我发现匿名函数的表达能力比普通函数要差,所以我认为我不会将它们用于这个精确的目的。
不过,这是个人喜好,并且对于这是好还是坏没有共识。
That depends on whether or not you're intending to re-use that function in more than just one view. If you do, you might want to give the function a name, and put it somewhere so that multiple views can use it. If you don't want to use that function ever again, an anonymous function would do. Then again, I find anonymous functions to be less expressive than normal functions, so I don't think I'd use them for this precise purpose.
It's personal preference, though, and there's no consensus on this being good or bad.
只要你有左手,为什么还要用右手刮左年呢?最好的方法是将其定义为某个地方的经典函数(myLeftyHelpers.php 或任何文件)。虽然你让我想知道,但我确实相信匿名函数的作用(如果它们在 PHP 中工作,从未尝试过)意味着充当函数指针,这意味着通过这种方式您可以将一个函数作为参数传递给另一个函数,这就是我看到它们的使用方式。如果有人认为我很累,请纠正我。
Why scratch your left year with your right hand, as long as you have a left hand to do it? Best way is to define this as a classic function somewhere (myLeftyHelpers.php or whatever file). Although you made me wonder, I really do belive that the role of anonymus functions(if they work in PHP, never tried) are meant to act as function pointers, meaning that this way you can pass a function as a parameters to another function, that's the way I see them used. If someone thinks I am worng please correct me.