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

How to get the attribute name of an object in php

怪我咯
Release: 2023-03-11 21:04:02
Original
4723 people have browsed it

There are many solutions:

1. Use the get_object_vars() method

Disadvantages: Only public

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

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));
     }
Copy after login

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");
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template