note 125648 added to function.get-object-vars
| From: | niemans at pbsolo dot nl | Date: | Sat, 02 Jan 2021 16:47:36 +0000 |
| Subject: | note 125648 added to function.get-object-vars | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-216051@lists.php.net to get a copy of this message | ||
You can use an anonymous class to return public variables from inside the class:
public function getPublicVars () {
$me = new class {
function getPublicVars($object) {
return get_object_vars($object);
}
};
return $me->getPublicVars($this);
}
Test script:
class Test {
protected $protected;
public $public;
private $private;
public function getAllVars () {
return call_user_func('get_object_vars', $this);
}
public function getPublicVars () {
$me = new class {
function getPublicVars($object) {
return get_object_vars($object);
}
};
return $me->getPublicVars($this);
}
}
$test = new Test();
print_r(get_object_vars($test)); // array("public" => NULL)
print_r($test->getAllVars()); // array("protected" => NULL, "public"
=> NULL, "private" => NULL)
print_r($test->getPublicVars()); // array("public" => NULL)
----
Server IP: 45.112.84.4
Probable Submitter: 45.91.159.165 (proxied: 92.209.42.127)
----
Manual Page -- http://php.net/manual/en/function.get-object-vars.php
Edit -- https://master.php.net/note/edit/125648
Del: integrated -- https://master.php.net/note/delete/125648/integrated
Del: useless -- https://master.php.net/note/delete/125648/useless
Del: bad code -- https://master.php.net/note/delete/125648/bad+code
Del: spam -- https://master.php.net/note/delete/125648/spam
Del: non-english -- https://master.php.net/note/delete/125648/non-english
Del: in docs -- https://master.php.net/note/delete/125648/in+docs
Del: other reasons-- https://master.php.net/note/delete/125648
Reject -- https://master.php.net/note/reject/125648
Search -- https://master.php.net/manage/user-notes.php