How to “echo” a class?

后端 未结 5 931
深忆病人
深忆病人 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 14:00

    You could try adding a toString method to your class. You can then echo some useful information, or call a render method to generate HTML or something!

    The __toString method is called when you do something like the following:

    echo $class;
    

    or

    $str = (string)$class;
    

    The example linked is as follows:

    foo = $foo;
        }
    
        public function __toString() {
            return $this->foo;
        }
    }
    
    $class = new TestClass('Hello');
    echo $class;
    ?>
    

提交回复
热议问题