PHP 8.5.0 Alpha 1 available for testing

Voting

: min(nine, eight)?
(Example: nine)

The Note You're Voting On

niemans at pbsolo dot nl
4 years ago
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)

<< Back to user notes page

To Top