Jump to content
Larry Ullman's Book Forums

Recommended Posts

a sidebar in Chapter 6 mentions that including a __toString method in the definition of a class can be an effective way to print the value of an object of the class if the object is used as a string; typically for debugging purposes. 

 

$a = new SomeClass();

echo $a;

 

I tried to invoke this in script 6.4 but received the following error:

 

Catchable fatal error Method User::__toString() must return a string value in Chapter6_interface.php.

 

Can anyone tell me what I'm doing wrong? I just displayed the pertinent segments of code to avoid confusion.

 

 

class User implements iCrud {
    
    private $_userId = NULL;
    private $_username = NULL;
 
function __toString()
    { }
 
    // Constructor takes an array of data:
    function __construct($data) {
        $this->_userId = uniqid();
        $this->_username = $data['username'];
    } // End of constructor.
 
 
 
 
// Identify the user information:
$user = array('username' => 'trout');
 
// Print a little introduction:
echo "<h2>Creating a New User</h2>";
 
// Create a new User:
$me = new User($user);
echo $me;
 
Link to comment
Share on other sites

 Share

×
×
  • Create New...