|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Nov 27 17:00:01 2025 UTC |
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;<?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