PHP的"var_dump"函数以递归方式输出对象的属性.我想知道是否有一种"转储"对象的方法,但不要在原始对象中转储递归对象.
原始转储:
object(Class_Name)#1 (3) {
["label":protected]=>
string(16) "My Label"
["name":protected]=>
string(16) "name"
["object":protected]=>
object(Class_Name)#2 (2) {
["id":protected]=>
NULL
["classes":protected]=>
array(0) {
}
}
}
Run Code Online (Sandbox Code Playgroud)
通缉转储:
object(Class_Name)#1 (3) {
["label":protected]=>
string(16) "My Label"
["name":protected]=>
string(16) "name"
["object":protected]=>
object(Class_Name)#2 (2) { ... }
}
Run Code Online (Sandbox Code Playgroud)
你可以写自己的
/**
* Schows all visible Vars of a Object, only in the first Level.
* To get private or protected we need to call the class
* in the Context of the Object ($this)
* @param object $obj The Object
* @param string|null $newLineCharacter The New Line Character (If null, it is based on \n for CLI or <br/> on web
* @return void
*/
function firstLevelVarDump($obj, $newLineCharacter = null) {
//Decide which new Line Character we use (Based on Loïc suggestion)
if ($newLineCharacter === null) {
$newLineCharacter = php_sapi_name() == 'cli' ? PHP_EOL : '<br/>';
}
//Get all visible Items
$data = get_object_vars($obj);
//Loop through each Item
foreach ($data as $key => $item) {
//Display Key + Type
echo $key . ' => ' . gettype($item);
//Extract Details, beased on the Type
if (is_string($item)) {
echo '(' . strlen($item) . ') "' . $item . '"';
} elseif (is_bool($item)) {
echo '(' . ($item ? 'true' : 'false') . ')';
} elseif (is_integer($item) || is_float($item)) {
echo '(' . $item . ')';
} elseif (is_object($item)) {
echo '(' . get_class($item) . ')';
}
//Line Break
echo $newLineCharacter;
}
}
Run Code Online (Sandbox Code Playgroud)