PHP - Class/Object get_mangled_object_vars() Function



The PHP Class/Object get_mangled_object_vars() function is used to return an array of mangled object properties. This function allows access to all properties, for example those with internal or special names.

Syntax

Below is the syntax of the PHP Class/Object get_mangled_object_vars() function −

array get_mangled_object_vars ( object $object )

Parameters

This function accepts $object parameter which is the object from which you want to fetch the mangled properties.

Return Value

The get_mangled_object_vars() function returns an associative array containing the object's properties, with the keys representing the mangled names of the properties.

PHP Version

First introduced in core PHP 7.4.0, the get_mangled_object_vars() function continues to function easily in PHP 8.

Example 1

This example shows how to extract and display a simple object's mangled properties, both private and public with the help of the PHP Class/Object get_mangled_object_vars() function.

<?php
   // Define a class here
   class MyClass {
      private $privateVar = 'Private';
      public $publicVar = 'Public';
   }
  
   $obj = new MyClass();
   $mangledVars = get_mangled_object_vars($obj);
   print_r($mangledVars);
?>

Output

Here is the outcome of the following code −

Array
(
    ["MyClassprivateVar"] => Private
)

Example 2

In the below PHP code we will use the get_mangled_object_vars() function and get the mangled names of protected properties.

<?php
   // Define a class here
   class MyClass {
      protected $protectedVar = 'Protected';
   }
  
   $obj = new MyClass();
   $mangledVars = get_mangled_object_vars($obj);
   print_r($mangledVars);
?> 

Output

This will generate the below output −

Array
(
   ["*protectedVar"] => Protected
)

Example 3

This example shows how to get the mangled properties using the get_mangled_object_vars() function from a subclass which inherits from a parent class.

<?php
   class ParentClass {
      private $parentPrivateVar = 'Parent Private';
   }
  
   class ChildClass extends ParentClass {
      private $childPrivateVar = 'Child Private';
   }
  
   $obj = new ChildClass();
   $mangledVars = get_mangled_object_vars($obj);
   print_r($mangledVars);
?> 

Output

This will create the below output −

Array
(
    ["ParentClassparentPrivateVar"] => Parent Private
    ["ChildClasschildPrivateVar"] => Child Private
)
php_function_reference.htm
Advertisements