Is there any function in smarty to check valid object of class in smarty?
Suppose $obj
is having some value or not.
How to check $obj
is object of 'TestClass' or not in smarty?
This is the way to check variable is object of specific class in Smarty.
if( true eq isset($obj) && true eq is_object($obj) && $obj instanceof 'TestClass' ){
//do something
}
try this
if($obj instanceof TestClass )
{
echo 'yes';
}
else
{
echo 'no';
}
this works in Smarty2 and Smarty3:
{if $obj instanceof TestClass}
…
{/if}
you can call php functions in the smarty code. Try this:
{if $customer instanceof Customer}
YES, instance of Customer
{else}
NO, Not an instance
{/if}
Also, it might be good idea to check if the variable is actually set before using it if the controller code has many paths:
{if isset($customer) && $customer instanceof Customer}
YES, instance of Customer
{else}
NO, Not an instance
{/if}
Function is_a
could be used for this.
{if is_a($customer, 'Customer')}
YES, instance of Customer
{else}
NO, Not an instance
{/if}
You can get the specific class of the object as well if needed with $obj|get_class
Example:
{if $animal instanceof Horse}
<span>Yup, it's a horse class.</span>
{else}
<span>It is actually a {{$animal|get_class}}</span>
{/if}
here is good example for this.
{if is_object($obj)}
{*=== your code ===*}
{else}
{*=== your code ===*}
{/if}
we can use is_object to know that this is an object or not.
hope it will help someone..