PHP Question: Print Object

Thursday, April 28, 2011 - 12:29

Question

The following code was executed.

1
2
3
4
5
6
7
<?php
class MyClass {
  private $foo = "bar";
}
 
$myObject = new MyClass();
echo $myObject;

Which produced the following error.

1
2
3
4
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:

1
2
3
4
5
6
7
8
9
10
11
<?php
class MyClass {
  private $foo = "bar";
 
  public function __toString() {
    return $this->foo;
  }
}
 
$myObject = new MyClass();
echo $myObject;

This stops the error occuring 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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?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:

1
2
3
4
5
6
<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>
Category: 
philipnorton42's picture

Philip Norton

Phil is the founder and administrator of #! code and is an IT professional working in the North West of the UK.
Google+ | Twitter

Comments

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

Add new comment