Home  >  Article  >  Backend Development  >  How to implement var_dump function in php

How to implement var_dump function in php

藏色散人
藏色散人Original
2020-08-24 10:02:172820browse

How to implement the "var_dump" function in php: first create a php sample file; then define a reconstructDump method; then implement the "var_dump" function through a for loop.

How to implement var_dump function in php

Recommended: "PHP Video Tutorial"

PHP implements a function similar to the var_dump function!

<?php
    function reconstructDump() {
        $args   = func_num_args();
        for($i = 0;$i < $args; $i ++) {
            $param = func_get_arg($i);
            switch(gettype($param)) {
                case &#39;NULL&#39; :
                    echo &#39;NULL&#39;;
                    break;
                case &#39;boolean&#39; :
                    echo ($param ? &#39;bool(true)&#39; : &#39;bool(false)&#39;);
                    break;
                case &#39;integer&#39; :
                    echo "int($param)";
                    break;
                case &#39;double&#39; :
                    echo "float($param)";
                    break;
                case &#39;string&#39; :
                    dumpString($param);
                    break;
                case &#39;array&#39; :
                    dumpArr($param);
                    break;
                case &#39;object&#39; :
                    dumpObj($param);
                    break;
                case &#39;resource&#39; :
                    echo &#39;resource&#39;;
                    break;
                default :
                    echo &#39;UNKNOWN TYPE&#39;;
                    break;
            }
        }
    }
 
 
function dumpString($param) {
    $str = sprintf("string(%d) %s",strlen($param),$param);
    echo $str;
}
 
function dumpArr($param) {
    $len = count($param);
    echo "array($len) {\r\n";
    foreach($param as $key=>$val) {
        if(is_array($val)) {
            dumpArr($val);
        } else {
            echo sprintf(&#39;["%s"] => %s(%s)&#39;,$key,gettype($val),$val);
        }
    }
    echo "}\r\n";
}
 
function dumpObj($param) {
    $className = get_class($param);
    $reflect = new ReflectionClass($param);
    $prop = $reflect->getDefaultProperties();
    echo sprintf("Object %s #1(%d) {\r\n",$className,count($prop));
    foreach($prop as $key=>$val) {
        echo "[\"$key\"] => ";
        reconstructDump($val);
    }
    echo "}";
}
 
class MyClass
{
    protected $_name;
    function test()
    {
        echo "hello";
    }
}
 
$str    = "test";
reconstructDump(new MyClass(),$str);
echo "\r\n";
$arr2   = array(
    "1"     => "Ddaddad",
    "one"   => array("two" => "Dddd" ),
    "three" => 1
);
reconstructDump($arr2); 
reconstructDump(1,true,null);
exit;

The above is the detailed content of How to implement var_dump function in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn