Link to home
Start Free TrialLog in
Avatar of Unimatrix_001
Unimatrix_001Flag for United Kingdom of Great Britain and Northern Ireland

asked on

Getting type of an array element.

Hello:

class A{
}

class B{
}

function MyFunction(){
     $mArray=array();
     $mArray[]=new A();
     $mArray[]=new B();
     return $mArray;
}

Open in new window


I know how many elements the array returned from MyFunction has, but is there a way I can check if an element is of type A or B?

Thanks,
Uni
ASKER CERTIFIED SOLUTION
Avatar of Beverley Portlock
Beverley Portlock
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Unimatrix_001

ASKER

That's the ticket. :)
<?php

class A{
}

class B{
}

function MyFunction(){
     $mArray=array();
     $mArray[]=new A();
     $mArray[]=new B();
     return $mArray;
}

$obj = MyFunction();
foreach( $obj as $o )
     if ( is_object($o ) )
          echo get_class( $o ) . "<br/>";

produces

A
B