Home  >  Article  >  Backend Development  >  How to get the attribute name of an object in php

How to get the attribute name of an object in php

怪我咯
怪我咯Original
2017-06-28 14:00:244697browse

There are many solutions:

1. Use the get_object_vars() method

Disadvantages: Only public

//只显示public的
var_dump(get_object_vars($test));

processing can be displayed : Define a public method in the class, which can be called by the external object to display all properties (except static properties)

function showAllProperties2(){
         var_dump(get_object_vars($this));
     }

2. Use ReflectionClass class

to get all attribute names

//显示static的
class ABC
{
    public static $instance='hello';
}
 
function get_all_static($className)
{
    $r = new ReflectionClass($className);
    var_dump($r->getProperties());
}
 
get_all_static("ABC");

PS:

PHP: Reflection API

Use of PHP’s reflection classes ReflectionClass and ReflectionMethod Example


The above is the detailed content of How to get the attribute name of an object in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn