Solved

Getting type of an array element.

Posted on 2011-03-01
3
276 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
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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 …

776 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