note 126846 added to closure.bindto
| From: | OlexandrKalaidzhy at main dot php dot net | Date: | Sat, 12 Feb 2022 11:51:23 +0000 |
| Subject: | note 126846 added to closure.bindto | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-217603@lists.php.net to get a copy of this message | ||
Get all object vars without using Reflection:
<?php
declare(strict_types=1);
class A
{
private $foo = 'foo';
protected $bar = 'bar';
public $buz = 'buz';
}
function get_object_vars_all($object): array
{
if (!\is_object($object)) {
throw new \InvalidArgumentException(sprintf('The argument should be an object,
"%s" given.', get_debug_type($object)));
}
$closure = function () {
return get_object_vars($this);
};
return $closure->bindTo($object, $object)();
}
$a = new A();
var_dump(get_object_vars($a));
var_dump(get_object_vars_all($a));
?>
The output:
array(1) {
["buz"]=>
string(3) "buz"
}
array(3) {
["foo"]=>
string(3) "foo"
["bar"]=>
string(3) "bar"
["buz"]=>
string(3) "buz"
}
----
Server IP: 45.112.84.4
Probable Submitter: 80.90.5.135 (proxied: 95.158.54.173)
----
Manual Page -- https://php.net/manual/en/closure.bindto.php
Edit -- https://main.php.net/note/edit/126846
Del: integrated -- https://main.php.net/note/delete/126846/integrated
Del: useless -- https://main.php.net/note/delete/126846/useless
Del: bad code -- https://main.php.net/note/delete/126846/bad+code
Del: spam -- https://main.php.net/note/delete/126846/spam
Del: non-english -- https://main.php.net/note/delete/126846/non-english
Del: in docs -- https://main.php.net/note/delete/126846/in+docs
Del: other reasons-- https://main.php.net/note/delete/126846
Reject -- https://main.php.net/note/reject/126846
Search -- https://main.php.net/manage/user-notes.php