smarty 中是否有任何功能可以检查 smarty 中类的有效对象?
假设$obj是否有一些价值。
如何$obj在 smarty 中检查是否是“TestClass”的对象?
这是在 Smarty 中检查变量是否为特定类的对象的方法。
if( true eq isset($obj) && true eq is_object($obj) && $obj instanceof 'TestClass' ){
//do something
}
这适用于 Smarty2 和 Smarty3:
{if $obj instanceof TestClass}
  …
{/if}
试试这个
if($obj instanceof TestClass )
{
    echo 'yes';
}
else
{
    echo 'no';
}
这是一个很好的例子。
{if is_object($obj)}
  {*=== your code ===*}
{else}
  {*=== your code ===*}
{/if}
我们可以使用 is_object 来知道这是否是一个对象。
希望它会帮助某人..
您可以在 smarty 代码中调用 php 函数。试试这个:
{if $customer instanceof Customer}
    YES, instance of Customer
{else}
    NO, Not an instance
{/if}
此外,如果控制器代码有很多路径,那么在使用它之前检查变量是否实际设置可能是个好主意:
{if isset($customer) && $customer instanceof Customer}
    YES, instance of Customer
{else}
    NO, Not an instance
{/if}
函数is_a可以用于此。
{if is_a($customer, 'Customer')}
    YES, instance of Customer
{else}
    NO, Not an instance
{/if}
如果需要,您也可以获取对象的特定类$obj|get_class
例子:
{if $animal instanceof Horse}
    <span>Yup, it's a horse class.</span>
{else}
    <span>It is actually a {{$animal|get_class}}</span>
{/if}