Solved

Getting type of an array element.

Posted on 2011-03-01
3
281 Views
Last Modified: 2012-05-11
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
0
Comment
Question by:Unimatrix_001
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 500 total points
ID: 35006847
0
 
LVL 3

Author Closing Comment

by:Unimatrix_001
ID: 35006856
That's the ticket. :)
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 35006860
<?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
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question