简体   繁体   English

如何知道对象实例属于什么类? (php5)

[英]How to know what class is an object instance of? (php5)

In PHP 5, how do I figure out what class is an object instance of? 在PHP 5中,如何确定对象实例属于哪个类? 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. 我知道我可以与“ instanceof”运算符进行比较,但是当类名未知时,我似乎找不到如何继续的方法。

Thanks in advance! 提前致谢! :) :)

get_class(...) will tell you the class of which an object is an instance. get_class(...)会告诉您对象是实例的类。

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

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. 我能想到的最简单的方法是遍历get_defined_vars()返回的每个变量,并对每个变量运行一个instanceof Can be hugely expensive speed and memory wise, though. 但是,在速度和内存方面可能会非常昂贵。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM