?
Solved

Getting type of an array element.

Posted on 2011-03-01
3
Medium Priority
?
284 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 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

777 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