PHP Function Reference

PHP get_mangled_object_vars() Function



The PHP get_mangled_object_vars() function returns an associative array containing all non-static properties, regardless of visibility, of object. The keys are the member variable names, with a few notable exceptions: private variables have the class name prepended to the variable name, and protected variables have a * prepended to the variable name. These prepended values have NUL bytes on either side.

Syntax

get_mangled_object_vars(object)

Parameters

object Required. Specify an object instance.

Return Value

Returns an associative array containing all properties, regardless of visibility, of object.

Example: get_mangled_object_vars() example

The example below shows the usage of get_mangled_object_vars() function.

<?php
class myClass {
  private $a = 10;
  public $b = 20;
  public $c;
  protected $d = 30;
  static $e = 40;

  //class method
  public function test() {
    print_r(get_mangled_object_vars($this));
  }
}

$myObj = new myClass();

//external call
print_r(get_mangled_object_vars($myObj));

//internal call
$myObj->test();
?>

The output of the above code will be:

Array
(
    [myClassa] => 10
    [b] => 20
    [c] => 
    [*d] => 30
)
Array
(
    [myClassa] => 10
    [b] => 20
    [c] => 
    [*d] => 30
)

Example: using with derived class object

Consider the example below where this function is used with a derived class object.

<?php
class myClass {
  private $a = 10;
  public $b = 20;
  public $c;
  protected $d = 30;
  static $e = 40;

  //class method
  public function test() {
    print_r(get_mangled_object_vars($this));
  }
}

class newClass extends myClass {
  public $f = 50;
  private $g;
  static $h = 60;
  protected $i;
}

$myObj = new newClass();

//external call
print_r(get_mangled_object_vars($myObj));

//internal call
$myObj->test();
?>

The output of the above code will be:

Array
(
    [f] => 50
    [newClassg] => 
    [*i] => 
    [myClassa] => 10
    [b] => 20
    [c] => 
    [*d] => 30
)
Array
(
    [f] => 50
    [newClassg] => 
    [*i] => 
    [myClassa] => 10
    [b] => 20
    [c] => 
    [*d] => 30
)

❮ PHP Classes/Objects Reference

5