Christopher Bergin Posted February 9, 2013 Share Posted February 9, 2013 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 More sharing options...
Larry Posted February 9, 2013 Share Posted February 9, 2013 You actually need to write the __toString() method so that it does output a string representation of the object. In this case, you might have it output the user ID and username, with labels. 1 Link to comment Share on other sites More sharing options...
Recommended Posts