Solved

Getting type of an array element.

Posted on 2011-03-01
3
273 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
  • 2
3 Comments
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 500 total points
Comment Utility
0
 
LVL 3

Author Closing Comment

by:Unimatrix_001
Comment Utility
That's the ticket. :)
0
 
LVL 34

Expert Comment

by:Beverley Portlock
Comment Utility
<?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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now