|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #47781 __toString() problem with an integer return
Submitted: 2009-03-25 22:42 UTC Modified: 2009-03-25 22:53 UTC
From: fabien dot meynard at supinfo dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.2.9 OS: macos x
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: fabien dot meynard at supinfo dot com
New email:
PHP Version: OS:


 [2009-03-25 22:42 UTC] fabien dot meynard at supinfo dot com
When we want to use __toString() method for an object we can't return a integer, that's a problem for example if we have a class named Zip and __toString() method have to return zipcode.

I my example, seconde class represent the fix in php : we have to force implicit conversion from integer to string

Reproduce code:
Class Zip
  protected $zipcode = 33000;

  public function __toString() 
    return $this->zipcode;

$zip = new Zip();
echo  'Zip : ' .$zip;

// and now the fix
Class Zip2
  protected $zipcode = 33000;
  public function __toString()
    return (string) $this->zipcode;

Expected result:
Zip : 33000

Actual result:
Catchable fatal error: Method Zip::__toString() must return a string value in /Users/fabienmeynard/- on line 13


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2009-03-25 22:46 UTC]
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at and the instructions on how to report
a bug at

 [2009-03-25 22:53 UTC] fabien dot meynard at supinfo dot com
I'm sorry but in documentation of __toString() we can't find any word about the non-implicit conversion from integer to string.
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Wed Mar 22 22:03:41 2023 UTC