# RFC: __debugInfo() magic method

- Status: Implemented (PHP 5.6)
- Authors: Sara Golemon, pollita@php.net
- Date: 2014-01-21
- Version: 1.1
- Original: https://wiki.php.net/rfc/debug-info
- Page: https://externals.io/rfc/debug-info
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

## Votes

### Merge ___debugInfo() (closed)

Yes (5.6): 23 · Yes (5.7): 1 · No: 3

Voters: aharvey (Yes (5.6)), ajf (Yes (5.6)), bwoebi (Yes (5.6)), chobieeee (Yes (5.6)), chregu (Yes (5.6)), dsp (Yes (5.6)), fa (Yes (5.6)), googleguy (No), indeyets (Yes (5.6)), jpauli (Yes (5.6)), jwage (Yes (5.6)), kassner (Yes (5.6)), krakjoe (Yes (5.6)), levim (No), lstrojny (Yes (5.6)), malukenho (Yes (5.6)), mbeccati (Yes (5.6)), peehaa (Yes (5.6)), pollita (Yes (5.6)), rdlowrey (Yes (5.6)), remi (Yes (5.6)), salathe (Yes (5.7)), sebastian (Yes (5.6)), stas (No), treffynnon (Yes (5.6)), willfitch (Yes (5.6)), yohgaki (Yes (5.6))

### Name ___debugInfo() (closed)

__debugInfo(): 23 · __debug(): 1

Voters: aharvey (__debugInfo()), ajf (__debug()), bwoebi (__debugInfo()), chobieeee (__debugInfo()), chregu (__debugInfo()), dsp (__debugInfo()), fa (__debugInfo()), indeyets (__debugInfo()), jpauli (__debugInfo()), krakjoe (__debugInfo()), levim (__debugInfo()), lstrojny (__debugInfo()), malukenho (__debugInfo()), mariuz (__debugInfo()), mbeccati (__debugInfo()), nikic (__debugInfo()), pollita (__debugInfo()), rdlowrey (__debugInfo()), remi (__debugInfo()), salathe (__debugInfo()), sebastian (__debugInfo()), stas (__debugInfo()), willfitch (__debugInfo()), yohgaki (__debugInfo())

## Discussions on the mailing list

- [\[RFC\] __debug_info()](https://externals.io/message/71381.md) — started on 2014-01-21, 29 messages
- [\[RFC\] \[VOTE\] __debugInfo()](https://externals.io/message/72112.md) — started on 2014-02-03, 55 messages

---

## Proposal

This RFC is for exposing the “get\_debug\_info” API already available to internal classes (written in C) to PHP classes via a magic method called \_\_debugInfo().

Example Usage:

```
class File {
  // "Resource(stream)" isn't all that useful
  private $fp;

  // But all the stream meta data is
  public function __debugInfo() {
    return $this->fp ? stream_get_meta_data($fp) : [];
  }

  public function open($filename, $mode = 'r'){
    $this->fp = fopen($filename, $mode);
  }  
}

$f = new File;
var_dump($f); // object(File)#1 { }
$f->open('http://php.net');
var_dump($f);
/*
object(File)#1 {
  ["wrapper_type"]=>
  string(4) "http"
  ["stream_type"]=>
  string(10) "tcp_socket"
  etc...
*/
```

## Backward Incompatible Changes

Double underscore is a reserved namespace, so any classes which happen to implement this are the ones which need to change.

## Impact to Existing Extensions

Existing fallback behavior for internal classes not overriding get\_debug\_info is preserved via chaining to get\_properties when no \_\_debugInfo function is defined.

## Proposed PHP Version(s)

5.next

## Alternates under discussion

- Rather than use \_\_debugInfo() as the whole property info result, either merge it with the properties table or stuff it into a "\_\_debugInfo" property and return it with the full properties table.

IMO, this takes control away from the user and removes a particularly useful reason for having the \_\_debugInfo() magic method in the first place. Consider an object which points at itself (or more likely, another object which points back at the original). This normally results in a recursive loop. This is detected by the var\_dump() routine, but only after a couple cycles. A more intelligent \_\_debugInfo() output could avoid any iterations of the loop.

## Potential expansions of scope

- Add ReflectionObject::getDebugInfo() to return the result of zend\_object\_handlers-&gt;get\_debug\_info as an array of ReflectionParameter objects

## Revision History

- 1.1 (2014-01-22) - Changed name from \_\_debug\_info to \_\_debugInfo and added alternates/expansion headings
- 1.0 (2014-01-21) - Initial Revision

## Vote 1

- Vote start: 2014/02/03
- Vote end: 2014/02/17

Vote \[YES\] to merge the \_\_debugInfo() patch. See Vote 2 for options on what name it will have.

**Vote: Merge \_\_\_debugInfo()** — Yes (5.6): 23 · Yes (5.7): 1 · No: 3 (closed)

## Vote 2

- Vote start: 2014/02/03
- Vote end: 2014/02/17

If Vote 1 passes, what should the userland magic method be called?

**Vote: Name \_\_\_debugInfo()** — \_\_debugInfo(): 23 · \_\_debug(): 1 (closed)

## Implementation

<https://github.com/sgolemon/php-src/compare/debug.info>
