php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #60452 redefine __toString
Submitted: 2011-12-06 21:28 UTC Modified: 2012-01-17 12:26 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: kashpur dot kostya at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.3.8 OS: Windows7
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
3 + 30 = ?
Subscribe to this entry?

 
 [2011-12-06 21:28 UTC] kashpur dot kostya at gmail dot com
Description:
------------
I can`t redefine __toString

Test script:
---------------
class A extends \SplFileInfo
{
...
   public function __toString()
   {
       return 'it`s A';
   }
...
}



Expected result:
----------------
it`s A

Actual result:
--------------
path/to/the/file.php

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-12-07 02:40 UTC] laruence@php.net
-Status: Open +Status: Feedback
 [2011-12-07 02:40 UTC] laruence@php.net
Thank you for this bug report. To properly diagnose the problem, we
need a short but complete example script to be able to reproduce
this bug ourselves. 

A proper reproducing script starts with <?php and ends with ?>,
is max. 10-20 lines long and does not require any external 
resources such as databases, etc. If the script requires a 
database to demonstrate the issue, please make sure it creates 
all necessary tables, stored procedures etc.

Please avoid embedding huge scripts into the report.

I can not reproduce what you described with following assuming script:
<?php
class A extends \SplFileInfo
{
    public function __toString()
    {
        return 'it`s A';
    }
}


$l = new A(PHP_SELF);

echo $l;
 [2011-12-08 20:45 UTC] kashpur dot kostya at gmail dot com
<?php
class File extends \SplFileInfo
{
    public function __construct($file)
    {
        parent::__construct($file);
    }

    public function __toString()
    {
         return 'It`s a file';
    }
}

final class A
{
    private $_file;

    public function __construct(File $file)
    {
        $this->_file = $file;
    }

    public function __toString()
    {
        return $this->getFile() . '-magic';
    }

    public function getFile()
    {
        return $this->_file;
    }
}

$a = new A(new File('file_name.php'));
print $a;

?>

*************************************
Actual result:
--------------
file_name.php-magic

Maybe the trick is in this magic '-magic'
Sorry for taftologiya

If I removes '-magic' string PHP would show me "Catchable fatal error: Method A::__toString() must return a string value in..."

And with magic it would call \SplFileInfo::__toString() not File::__toString()

Expected result:
----------------
It`s a file

p.s. hope this code is enoughе to properly diagnose the problem
 [2011-12-08 20:45 UTC] kashpur dot kostya at gmail dot com
-Status: Feedback +Status: Open
 [2012-01-17 12:26 UTC] mike@php.net
-Status: Open +Status: Bogus
 [2012-01-17 12:26 UTC] mike@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

__toString() MUST return a string. You return an object that has __toString 
implemented instead.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 07:01:27 2024 UTC