php 如何获取对象或类名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2602818/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:05:08  来源:igfitidea点击:

How to get the object or class name?

phparraysobject

提问by justjoe

Right now, I have this code where $obj_arrmaybe contain array and an object.

现在,我有这个代码,其中$obj_arr可能包含数组和一个对象。

$obj_temp = array ($obj_identity, $arr_user, $obj_locale, $arr_query); 

foreach ($obj_temp as $maybe_arr) {
        if (is_array($maybe_arr)) :
            $name = (string) key($maybe_arr);
        if (is_object($maybe_arr)) :    
            ???? // how to retrieve a class name ?
        endif;  

        $obj_arr[$name] = $maybe_arr;

}



obj_will_be_extract($obj_arr);

function obj_will_be_extract($obj_arr) {
    extract($obj_arr);

    //Do the rest

}

I need to create an array consist of combination of objects and arrays. Cause I need to extract it, then how to get an object name?

我需要创建一个由对象和数组组合组成的数组。因为我需要提取它,那么如何获取对象名称?

回答by Gumbo

Use get_classto get the class name of an object.

使用get_class获取对象的类名。

回答by pleerock

Since PHP 5.5 you can use MyAction::classstatement, so you can get a class name without class initialization

从 PHP 5.5 开始可以使用 MyAction::classstatement,所以不用类初始化就可以得到类名