Smarty - register_object和assign_by_ref之间的区别

时间:2009-07-29 10:53:01

标签: php smarty

这是我将PHP对象暴露给Smarty模板的两个选项。我知道两者之间存在语法差异,但我找不到任何关于为什么要使用其中一个的信息。任何人都可以解释这些差异吗?

谢谢,

詹姆斯。

2 个答案:

答案 0 :(得分:2)

如果使用register_object(),则可以限制可以调用的方法,这也意味着您使用不同的(更像Smarty的)语法调用方法:

<?php
// registering the object (will be by reference)
$smarty->register_object('foobar',$myobj);

// if we want to restrict access to certain methods or properties, list them
$smarty->register_object('foobar',$myobj,array('meth1','meth2','prop1'));

模板:

{* access our registered object *}
{foobar->meth1 p1='foo' p2=$bar}

来自http://www.smarty.net/manual/en/advanced.features.php

答案 1 :(得分:2)

我认为这个smarty文档页面可以帮助您:http://www.smarty.net/manual/en/advanced.features.php#advanced.features.objects

我认为我不能说比那里说的更多;基本上:

  • assign允许您将数据发送到模板;其中一些数据可以是对象
  • register_object允许更多;最重要的似乎是您可以从模板文件中指定可以使用对象的哪些方法

第二种可能性还提供了更像“类似对象”的语法,看起来像这个(从文档复制的示例)

{* access our registered object *}
{foobar->meth1 p1='foo' p2=$bar}

{* you can also assign the output *}
{foobar->meth1 p1='foo' p2=$bar assign='output'}
the output was {$output}

{* access our assigned object *}
{$myobj->meth1('foo',$bar)}


所以,我会说:选择最适合您需求的那个; - )

相关问题