Edit report at https://bugs.php.net/bug.php?id=60452&edit=1
ID: 60452
User updated by: kashpur dot kostya at gmail dot com
Reported by: kashpur dot kostya at gmail dot com
Summary: redefine __toString
-Status: Feedback
+Status: Open
Type: Bug
Package: *General Issues
Operating System: Windows7
PHP Version: 5.3.8
Block user comment: N
Private report: N
New Comment:
<?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
Previous Comments:
------------------------------------------------------------------------
[2011-12-07 02:40:59] [email protected]
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