php.net |  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
 [2009-03-25 22:42 UTC] fabien dot meynard at supinfo dot com
Description:
------------
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:
---------------
<?php 
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


Patches

Pull Requests

History

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


 [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.

http://fr.php.net/manual/en/language.oop5.magic.php#language.oop5.magic.tostring
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 26 14:01:30 2024 UTC