Bug #60452 [Opn->Bgs]: redefine __toString
| From: | mike@php.net | Date: | Tue, 17 Jan 2012 12:26:24 +0000 |
| Subject: | Bug #60452 [Opn->Bgs]: redefine __toString | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-166534@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=60452&edit=1
ID: 60452
Updated by: mike@php.net
Reported by: kashpur dot kostya at gmail dot com
Summary: redefine __toString
-Status: Open
+Status: Bogus
Type: Bug
Package: *General Issues
Operating System: Windows7
PHP Version: 5.3.8
Block user comment: N
Private report: N
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2011-12-08 20:45:40] 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-07 02:40:59] 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-06 21:28:35] 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
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=60452&edit=1