Suppose I pass an array, which can contain instances of two different classes, to View template, which contain table markup. Each item will be presented as table row, so I need to check it's class name in order to include appropriate row partial. So far I can see only two ways to achieve this goal:
- compare class name using
get_class
function - add
isFoo()
method to both classes, which will returntrue
if object's class isFoo
I don't like the second option, because it's harder to maintain in situations where I have more than two classes, but I wonder if first option is commonly used and get_class
function is appropriate to be placed in View templates.
Example in laravel's blade template:
/* catalog.cycle_rows.blade.php */
@foreach($cycles as $cycle)
@include('catalog.cycle_row')
@if( $cycle->subcycles )
@include('catalog.cycle_rows', ['cycles' => $cycle->subcycles])
@else
@foreach($cycle->cycleItems as $cycleItem)
@if($cycleItem->isBlock()) // here's the check
@include('catalog.block_row', ['cycle' => $cycleItem])
@else
@include('catalog.cycle_item_row')
@endif
@endforeach
@endif
@endforeach