PHP get_mangled_object_vars() Function

What is PHP get_mangled_object_vars() Function?

If you want to get all the properties (except the static variable) of an object, use get_mangled_object_vars() function.

Syntax:

get_mangled_object_vars(object)

Parameters:

The Function has 1 parameter which is required-

object (Required): It specifies an object.

Return Values:

The function returns an associative array containing all the properties. The keys are named according to the variable types as follows-

  • For “Public” variable – the key is the “variable name”.
  • For “Private” variable – the key is the “Class name variable name” (no gap)
  • For “Protected” variable – the key is the “* variable name” (no gap)

Check example 1.

Examples:

Example:

<pre>
<?php
class human {
  private $hormon = "Yes";
  public $hand = 2;
  public $leg = 2;
  protected $horn = "No";
  static $tail = "No";
}
$humanObj = new human();
print_r(get_mangled_object_vars($humanObj));
?>
</pre>

Output:

Array
(
[humanhormon] => Yes
[hand] => 2
[leg] => 2
[*horn] => No
)

Difference between get_mangled_object_vars() and get_object_vars() Function:

get_mangled_object_vars() function returns all the varfables of an object regardless of their visibilities – public, private, and protected. On the other hand get_object_vars() function only returns public variables.

PHP Version Support:

PHP 7 >= 7.4.0, PHP 8

Summary: PHP get_mangled_object_vars() Function

get_mangled_object_vars() is a class/object type function in PHP. Use this function when you want to see all the properties of an object.

Reference:

https://www.php.net/manual/en/function.get-mangled-object-vars.php