Skip to content

No properties dumping for instances of SPL \ArrayObject. #413

@tomFlidr

Description

@tomFlidr

Version: all

Bug Description

No properties dumping for instances extended from SPL \ArrayObject:
https://www.php.net/manual/en/class.arrayobject
There is no variable content rendered by dump()/bdump()
functions for instances extended from SPL class \ArrayObject.

Steps To Reproduce

class Config extends \ArrayObject {
	// ...
	protected $fullPath = NULL;
	protected $store = [];
	public function __construct ($fullPath, $data) {
		$this->fullPath = $fullPath;
		$this->store = $data;
	}
	
	public function __get ($key) {
		if (array_key_exists($key, $this->store))
			return $this->store[$key];
		return NULL;
	}
	public function __set ($key, $value) {
		return $this->store[$key] = $value;
	}
	public function __isset ($key) {
		return isset($this->store[$key]);
	}
	public function __unset ($key) {
		if (isset($this->store[$key])) 
			unset($this->store[$key]);
	}
	public function getIterator () {
		return new \ArrayIterator($this->store);
	}
	public function offsetSet ($offset, $value) {
		if ($offset === NULL) {
			$this->store[] = $value;
		} else {
			$this->store[$offset] = $value;
		}
	}
	public function offsetGet ($offset) {
		return isset($this->store[$offset]) 
			? $this->store[$offset] 
			: NULL;
	}
	public function offsetExists ($offset) {
		return isset($this->store[$offset]);
	}
	public function offsetUnset ($offset) {
		unset($this->store[$offset]);
	}
	public function count () {
		return count($this->store);
	}
}


// let's create and instanceof class `Config`:
$cfg = new Config(__DIR__ . '/cfg.ini', [
	'ga' => true,
	'db' => [
		'driver'=> 'mysql',
		'host'	=> 'localhost',
		'port'	=> 3306,
		'db'	=> 'cdcol',
		'pass'	=> NULL
	]
]);

echo '<pre>';

// working ok in loops and other:
$cfg['something'] = 'more';
dump(count($cfg));
dump(isset($cfg['ga']));
dump(isset($cfg['something']));
unset($cfg['something']);
dump(isset($cfg['something']));
foreach ($cfg as $key => $value) {
	dump([$key, $value]);
}

// Working OK to dump it by simple `var_dump()`:
//var_dump($cfg);

// but it's not possible to dump with tracy:
dump($cfg);
bdump($cfg);

Expected Behavior

The expected behaviour is to dump Config::$fullPath
and Config::$store properties like var_dump() does.

Possible Solution

In class: Dumper
Method: exportObject()
The method could be changed like this:

	//////////////// FIXED - return type `iterable` (PHP >= 7.1):
	//private function exportObject($obj): array
	private function exportObject($obj): iterable
	{
		foreach ($this->objectDumpers as $type => $dumper) {
			if (!$type || $obj instanceof $type) {
				return $dumper($obj);
			}
		}

		if ($this->debugInfo && method_exists($obj, '__debugInfo')) {
			return $obj->__debugInfo();
		}
		
		//////////////// ADDED - condition:
		if ($obj instanceof \ArrayObject) return $obj;
		
		return (array) $obj;
	}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions