0

In PHP 5, how do I figure out what class is an object instance of? I know I can compare with the "instanceof" operator, but I don't seem to find how to proceed when the Class name is unknown.

Thanks in advance! :)

2
  • 1
    You probably have a design problem if you need this. The reason I say this, is because you apparently cannot bound the set of useful answers. Hence, there must be at least on object whose type you don't know up front, yet with which you want to do something useful. But how can you do something useful to an object whose type you don't know at all? Commented Dec 18, 2009 at 15:03
  • Maybe we do have a design problem (our codebase is pretty old), but I didn't want to "do" something "to" an object. In our ActiveTable class we have a method that always generates a log entry, but I wanted to omit this log entry only if the object that inherited from the class that inherited from ActiveTable was of certain type :) Commented Dec 21, 2009 at 18:22

2 Answers 2

9

get_class(...) will tell you the class of which an object is an instance.

http://www.php.net/manual/en/function.get-class.php

Sign up to request clarification or add additional context in comments.

2 Comments

add the Reflection API to the list
How could I miss that! Thank you very much :-) that's exactly what I meant.
0

You mean, you want to find all objects that are of a certain class?

The simplest way I can think of is to go through each variable returned by get_defined_vars() and run an instanceof on each of them. Can be hugely expensive speed and memory wise, though.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.