Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ext/standard/basic_functions.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -1475,6 +1475,8 @@ function var_export(mixed $value, bool $return = false): ?string {}

function debug_zval_dump(mixed $value, mixed ...$values): void {}

function var_representation(mixed $value, int $flags = 0): string {}

function serialize(mixed $value): string {}

function unserialize(string $data, array $options = []): mixed {}
Expand Down
9 changes: 8 additions & 1 deletion ext/standard/basic_functions_arginfo.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: 7540039937587f05584660bc1a1a8a80aa5ccbd1 */
* Stub hash: 533808ec59e5d4713134fe8c76a5a4df2ee872cb */

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_set_time_limit, 0, 1, _IS_BOOL, 0)
ZEND_ARG_TYPE_INFO(0, seconds, IS_LONG, 0)
Expand Down Expand Up @@ -2157,6 +2157,11 @@ ZEND_END_ARG_INFO()

#define arginfo_debug_zval_dump arginfo_var_dump

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_var_representation, 0, 1, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, value, IS_MIXED, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, flags, IS_LONG, 0, "0")
ZEND_END_ARG_INFO()

#define arginfo_serialize arginfo_gettype

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_unserialize, 0, 1, IS_MIXED, 0)
Expand Down Expand Up @@ -2811,6 +2816,7 @@ ZEND_FUNCTION(convert_uudecode);
ZEND_FUNCTION(var_dump);
ZEND_FUNCTION(var_export);
ZEND_FUNCTION(debug_zval_dump);
ZEND_FUNCTION(var_representation);
ZEND_FUNCTION(serialize);
ZEND_FUNCTION(unserialize);
ZEND_FUNCTION(memory_get_usage);
Expand Down Expand Up @@ -3463,6 +3469,7 @@ static const zend_function_entry ext_functions[] = {
ZEND_FE(var_dump, arginfo_var_dump)
ZEND_FE(var_export, arginfo_var_export)
ZEND_FE(debug_zval_dump, arginfo_debug_zval_dump)
ZEND_FE(var_representation, arginfo_var_representation)
ZEND_FE(serialize, arginfo_serialize)
ZEND_FE(unserialize, arginfo_unserialize)
ZEND_FE(memory_get_usage, arginfo_memory_get_usage)
Expand Down
1 change: 1 addition & 0 deletions ext/standard/php_var.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ PHP_MINIT_FUNCTION(var);
PHPAPI void php_var_dump(zval *struc, int level);
PHPAPI void php_var_export(zval *struc, int level);
PHPAPI void php_var_export_ex(zval *struc, int level, smart_str *buf);
PHPAPI void php_var_representation_ex(zval *struc, int level, smart_str *buf);

PHPAPI void php_debug_zval_dump(zval *struc, int level);

Expand Down
150 changes: 150 additions & 0 deletions ext/standard/tests/general_functions/var_representation1.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
--TEST--
Test var_representation() function
--FILE--
<?php
function dump($value): void {
echo var_representation($value), "\n";
$output = var_representation($value, VAR_REPRESENTATION_SINGLE_LINE);
echo "oneline: $output\n";
}
dump(null);
dump(false);
dump(true);
dump(0);
dump(0.0);
dump(new stdClass());
dump((object)['key' => "\$value\tsecond"]);
dump([]);
dump([1,2,3]);
dump(new ArrayObject(['a', ['b']]));
dump("Content-Length: 42\r\n");
// use octal
dump(["Foo\0\r\n\r\001\x19test quotes and special characters\$b'\"\\" => "\0"]);
// does not escape or check validity of bytes "\80-\ff" (e.g. utf-8 data)
dump("▜");
dump((object)["Foo\0\r\n\r\001" => true]);
echo "STDIN is dumped as null, like var_export\n";
dump(STDIN);
echo "The ascii range is encoded as follows:\n";
echo var_representation(implode('', array_map('chr', range(0, 0x7f)))), "\n";
echo "Recursive objects cause a warning, like var_export\n";
$x = new stdClass();
$x->x = $x;
dump($x);
echo "Recursive arrays cause a warning, like var_export\n";

$a = [];
$a[0] = &$a;
$a[1] = 'other';
dump($a);
$ref = 'ref shown as value like var_export';
dump([(object)['key' => (object)['inner' => [1.0]], 'other' => &$ref]]);
class Example {
public $untyped1;
public $untyped2 = null;
public $untyped3 = 3;
public int $typed1;
public array $typed2 = [];
// rendering does not depend on existence of public function __set_state(), like var_export.
}
$x = new Example();
unset($x->untyped1); // unset properties/uninitialized typed properties are not shown, like var_export
$x->typed2 = [new Example()];
dump($x);
?>
--EXPECTF--
null
oneline: null
false
oneline: false
true
oneline: true
0
oneline: 0
0.0
oneline: 0.0
(object) []
oneline: (object) []
(object) [
'key' => "\$value\tsecond",
]
oneline: (object) ['key' => "\$value\tsecond"]
[]
oneline: []
[
1,
2,
3,
]
oneline: [1, 2, 3]
\ArrayObject::__set_state([
'a',
[
'b',
],
])
oneline: \ArrayObject::__set_state(['a', ['b']])
"Content-Length: 42\r\n"
oneline: "Content-Length: 42\r\n"
[
"Foo\x00\r\n\r\x01\x19test quotes and special characters\$b'\"\\" => "\x00",
]
oneline: ["Foo\x00\r\n\r\x01\x19test quotes and special characters\$b'\"\\" => "\x00"]
'▜'
oneline: '▜'
(object) [
"Foo\x00\r\n\r\x01" => true,
]
oneline: (object) ["Foo\x00\r\n\r\x01" => true]
STDIN is dumped as null, like var_export

Warning: var_representation does not handle resources in %s on line 3
null

Warning: var_representation does not handle resources in %s on line 4
oneline: null
The ascii range is encoded as follows:
"\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !\"#\$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\x7f"
Recursive objects cause a warning, like var_export

Warning: var_representation does not handle circular references in %s on line 3
(object) [
'x' => null,
]

Warning: var_representation does not handle circular references in %s on line 4
oneline: (object) ['x' => null]
Recursive arrays cause a warning, like var_export

Warning: var_representation does not handle circular references in %s on line 3
[
null,
'other',
]

Warning: var_representation does not handle circular references in %s on line 4
oneline: [null, 'other']
[
(object) [
'key' => (object) [
'inner' => [
1.0,
],
],
'other' => 'ref shown as value like var_export',
],
]
oneline: [(object) ['key' => (object) ['inner' => [1.0]], 'other' => 'ref shown as value like var_export']]
\Example::__set_state([
'untyped2' => null,
'untyped3' => 3,
'typed2' => [
\Example::__set_state([
'untyped1' => null,
'untyped2' => null,
'untyped3' => 3,
'typed2' => [],
]),
],
])
oneline: \Example::__set_state(['untyped2' => null, 'untyped3' => 3, 'typed2' => [\Example::__set_state(['untyped1' => null, 'untyped2' => null, 'untyped3' => 3, 'typed2' => []])]])
Loading