How to “echo” a class?

后端 未结 5 968
深忆病人
深忆病人 2021-01-17 13:04

This is probably really easy but I can\'t seem to figure out how to print/echo a class so I can find out some details about it.

I know this doesn\'t work, but this i

5条回答
  •  没有蜡笔的小新
    2021-01-17 13:41

    To get more detailed info out of your class (if you want to know what's available to a child class for example), you can add a debug() method.

    Here's an example class with such a method that I use that prints out the methods, default vars, and instance vars in a nice structured way:

    privateVar = 'parent instance';
        }
        public function test(){}
        /**
         * Prints out detailed info of the class and instance.
         */
        public function debug(){
            $class = __CLASS__;
            echo "
    $class Methods:\n";
            var_dump(get_class_methods($class));
            echo "\n\n$class Default Vars:\n";
            var_dump(get_class_vars($class));
            echo "\n\n$class Current Vars:\n";
            var_dump($this);
            echo "
    "; } } class TestClassChild extends TestClass{ public function __construct(){ $this->privateVar = 'child instance'; } } $test = new TestClass(); $test2 = new TestClassChild(); $test->debug(); $test2->debug();

提交回复
热议问题