How to know what class is an object instance of? (

2019-01-29 07:38发布

问题:

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! :)

回答1:

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

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



回答2:

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.



标签: php oop