Use PHP's get_class_vars() function to get the attribute names in the class and return a comparison array containing the attribute names and values: pass in the object to be checked as a parameter. This function returns an associative array where the keys are property names and the values are property values.
How to use PHP function to return class attribute name
Introduction
In PHP The get_class_vars()
function can be used to get the attribute names in a class. This function returns an associative array where the keys are property names and the values are property values.
Syntax
get_class_vars(object $object)
Parameters
$object
: To get its attribute name Object. Return value
An associative array containing class attribute names and values.
Practical case
The following example shows how to use get_class_vars()
Function:
class Person { public $name; public $age; } $person = new Person(); $person->name = "John Doe"; $person->age = 30; $properties = get_class_vars($person); foreach ($properties as $property => $value) { echo "$property: $value\n"; }
Output:
name: John Doe age: 30
In the above example, the get_class_vars()
function returns a file containing two attribute names (name
and age
) and two attribute values (# Associative array of ##John Doe and
30).
The above is the detailed content of How does a PHP function return class attribute name?. For more information, please follow other related articles on the PHP Chinese website!