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';
}
スマートコードで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}
必要に応じて、オブジェクトの特定のクラスも取得できます$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}
これには関数is_a
を使用できます。
{if is_a($customer, 'Customer')}
YES, instance of Customer
{else}
NO, Not an instance
{/if}