PHP Question: Print Object

Question

The following code was executed.

<?php
class MyClass {
  private $foo = "bar";
}

$myObject = new MyClass();
echo $myObject;

Which produced the following error.

Catchable fatal error: Object of class MyClass could not be converted to string in test.php on line 7

Call Stack:
    0.0011     323736   1. {main}() /test.php:0

How can the code be simply changed to prevent this error and produce some form of result?









 

Answer

The easiest way to rewrite this code and to actually print out the object properly is to use the __toString() magic method. This method must return a string and is automatically called an object is printed out. The example code can therefore be rewritten like this:

<?php
class MyClass {
  private $foo = "bar";

  public function __toString() {
    return $this->foo;
  }
}

$myObject = new MyClass();
echo $myObject;

This stops the error occurring and will produce the output below.

bar

The __toString() function is useful for both debug purposes but also for decorating output. For example, we could have a User class that would print out the user's information, and a HtmlUser decorator class that prints out the user's information with surrounding HTML tags. This would be written like this:

<?php
class User {

  public $_username;
  public $_forename;
  public $_surname;
  public $_created;

  public function __construct() {
    $this->_username = 'user1';
    $this->_forename = 'John';
    $this->_surname = 'Doe';
    $this->_created = time();
  }

  public function __toString() {
    $output = '';
    
    $output .= "Username = " . $this->_username . "\n";
    $output .= "Forename = " . $this->_forename . "\n";
    $output .= "Surname = " . $this->_surname . "\n";
    $output .= "Created = " . $this->_created . "\n";
    
    return $output;
  }

}

class HtmlUser {

  protected $user;
  
  public function __construct(User $user) {
    $this->user = $user;
  }

  public function __toString() {
    $output = "<ul>\n";
    
    $output .= "\t<li><strong>Username</strong> " . $this->user->_username . "</li>\n";
    $output .= "\t<li><strong>Forename</strong> " . $this->user->_forename . "</li>\n";
    $output .= "\t<li><strong>Surname</strong> " . $this->user->_surname . "</li>\n";
    $output .= "\t<li><strong>Created</strong> " . $this->user->_created . "</li>\n";
    
    $output .= "</ul>";
    
    return $output;
  }

}

$user = new User();
$htmlUser = new HtmlUser($user);
echo $htmlUser;

This produces the following output:

<ul>
	<li><strong>Username</strong> user1</li>
	<li><strong>Forename</strong> John</li>
	<li><strong>Surname</strong> Doe</li>
	<li><strong>Created</strong> 1304196043</li>
</ul>

Comments

echo ( is_object( $myObject ) ) ? 'Sorry! We have an object here!' : $myObject ;

Permalink

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
2 + 11 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.