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 is what I'm trying to do:
<?php echo $class; ?>
What is the correct way to achieve something like this?
The echo() function outputs one or more strings. Note: The echo() function is not actually a function, so you are not required to use parentheses with it. However, if you want to pass more than one parameter to echo(), using parentheses will generate a parse error.
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:
<?php
// Declare a simple class
class TestClass
{
public $foo;
public function __construct($foo) {
$this->foo = $foo;
}
public function __toString() {
return $this->foo;
}
}
$class = new TestClass('Hello');
echo $class;
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With