Can somebody shed some light why:
<?php
echo new SplFileObject(FILE);
returns the first line of the file (in that case <?php
)?
SplFileInfo has the path and SplFileObject extends from it but it will return the current line. I do not really understand why and I tend to report this as a bug but I'm not sure.
--hakre
Can somebody shed some light why:
<?php echo new SplFileObject(__FILE__);
returns the first line of the file (in that case
<?php
)?SplFileInfo has the path and SplFileObject extends from it but it will
return the current line. I do not really understand why and I tend to
report this as a bug but I'm not sure.--hakre
--
SplFileObject->__toString() is an alias for current()
which will return the
current line of file
http://lxr.php.net/xref/PHP_5_3/ext/spl/spl_directory.c#2954
http://www.php.net/manual/en/splfileobject.tostring.php
it seems that the class synopsis at
http://www.php.net/manual/en/class.splfileobject.php is missing this method
so it is easy to think that it isn't provided by the SplFileObject so the
parent't method will be used, which isn't the case.
--
Ferenc Kovács
@Tyr43l - http://tyrael.hu
This feels like a bug to me. Why would SplFileObject::__toString return the
current line while SplFileInfo::__toString returns file path?
Can somebody shed some light why:
<?php echo new SplFileObject(__FILE__);
returns the first line of the file (in that case
<?php
)?SplFileInfo has the path and SplFileObject extends from it but it will
return the current line. I do not really understand why and I tend to
report this as a bug but I'm not sure.--hakre
--
SplFileObject->__toString() is an alias for
current()
which will return the
current line of file
http://lxr.php.net/xref/PHP_5_3/ext/spl/spl_directory.c#2954
http://www.php.net/manual/en/splfileobject.tostring.php
it seems that the class synopsis at
http://www.php.net/manual/en/class.splfileobject.php is missing this
method
so it is easy to think that it isn't provided by the SplFileObject so the
parent't method will be used, which isn't the case.--
Ferenc Kovács
@Tyr43l - http://tyrael.hu
Hi!
Can somebody shed some light why:
<?php echo new SplFileObject(__FILE__);
__toString is mapped to current()
for SplFileObject:
http://www.php.net/manual/en/splfileobject.current.php
it's not documented for some reason, I think it makes sense to file a
docs bug on that.
--
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
2013.01.30. 0:01, "Stas Malyshev" smalyshev@sugarcrm.com ezt írta:
Hi!
Can somebody shed some light why:
<?php echo new SplFileObject(__FILE__);
__toString is mapped to
current()
for SplFileObject:
http://www.php.net/manual/en/splfileobject.current.phpit's not documented for some reason, I think it makes sense to file a
docs bug on that.--
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227--
I've changed the docs so the __toString is listed in the method synapsis,
see http://docs.php.net/manual/en/class.splfileobject.php
----- Ursprüngliche Message -----
Von: Stas Malyshev smalyshev@sugarcrm.com
Gesendet: 0:00 Mittwoch, 30.Januar 2013
Betreff: Re: [PHP-DEV] echo new SplFileObject(FILE);__toString is mapped to
current()
for SplFileObject:
http://www.php.net/manual/en/splfileobject.current.phpit's not documented for some reason, I think it makes sense to file a
docs bug on that.
Thanks for your answer.
I know that there is code in there that does this and I also got into the know that it is not properly documented.
I just write this to clarify that I'm more interested in the why it has been coded that way.
It does not make much sense to me and I want to learn more.
Also I don't mean this explicit technically. I could blame the version control, pick the authors name and email that person; however some time has passed and more users are using it not only the original author so I ask in internals first. Just for clarification.
-- hakre
To be honest, it looks like __toString() was just added in there for the
sake of it without any real thought as to what casting an entier
SplFileObject to a string. This to me implies the entire object( i.e: the
entire file ) should be returned as a string rather than aliasing it to a
method because why would you cast something to a string if you can call
->current() anyway.
Since it's been baked into the object for some time now it can't even be
changed now.
I'd try to avoid this casting magic and stick with ->current() if you
actually mean it.
Thanks,
Paul Dragoonis.
----- Ursprüngliche Message -----
Von: Stas Malyshev smalyshev@sugarcrm.com
Gesendet: 0:00 Mittwoch, 30.Januar 2013
Betreff: Re: [PHP-DEV] echo new SplFileObject(FILE);__toString is mapped to
current()
for SplFileObject:
http://www.php.net/manual/en/splfileobject.current.phpit's not documented for some reason, I think it makes sense to file a
docs bug on that.Thanks for your answer.
I know that there is code in there that does this and I also got into the
know that it is not properly documented.I just write this to clarify that I'm more interested in the why it has
been coded that way.It does not make much sense to me and I want to learn more.
Also I don't mean this explicit technically. I could blame the version
control, pick the authors name and email that person; however some time has
passed and more users are using it not only the original author so I ask in
internals first. Just for clarification.-- hakre
Von: Paul Dragoonis dragoonis@gmail.com
Gesendet: 16:54 Mittwoch, 30.Januar 2013
To be honest, it looks like __toString() was just added in there for the sake of it without any real thought as to what casting an entier SplFileObject to a string. This to me implies the entire object( i.e: the entire file ) should be returned as a string rather than aliasing it to a method because why would you cast something to a string if you can call ->current() anyway.
Since it's been baked into the object for some time now it can't even be changed now.
Would this mean that changing it in PHP 5.5 (or 5.6) would not be an option because of the rules of backwards compatibility?
I'd try to avoid this casting magic and stick with ->current() if you actually mean it.
So do I. I mean, I often foreach anyway.
-- hakre
----- Ursprüngliche Message -----
Von: Stas Malyshev smalyshev@sugarcrm.com
Gesendet: 0:00 Mittwoch, 30.Januar 2013
Betreff: Re: [PHP-DEV] echo new SplFileObject(FILE);__toString is mapped to
current()
for SplFileObject:
http://www.php.net/manual/en/splfileobject.current.phpit's not documented for some reason, I think it makes sense to file a
docs bug on that.Thanks for your answer.
I know that there is code in there that does this and I also got into the
know that it is not properly documented.I just write this to clarify that I'm more interested in the why it has
been coded that way.It does not make much sense to me and I want to learn more.
Also I don't mean this explicit technically. I could blame the version
control, pick the authors name and email that person; however some time has
passed and more users are using it not only the original author so I ask in
internals first. Just for clarification.
I would guess the idea was that SplFileObject already implements the
Iterator interfaces and iterating the object would give you the lines of
the file, so echo $object should echo the current line.
But this isn't that strong of an argument, and I think that following
what SplFileInfo does would be more sensible (echoing the filename), but
I'm not sure change would worth breaking BC for.
--
Ferenc Kovács
@Tyr43l - http://tyrael.hu
Hi!
But this isn't that strong of an argument, and I think that following
what SplFileInfo does would be more sensible (echoing the filename), but
I'm not sure change would worth breaking BC for.
I don't see why it would be more sensible. It's different objects that
do different things - Info represents file name, more or less, while
Object represents file contents. I see no reason why it would only make
sense for Object to return filename, or why we should "fix" something
that is not broken.
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
2013.01.30. 19:16, "Stas Malyshev" smalyshev@sugarcrm.com ezt írta:
Hi!
But this isn't that strong of an argument, and I think that following
what SplFileInfo does would be more sensible (echoing the filename), but
I'm not sure change would worth breaking BC for.I don't see why it would be more sensible. It's different objects that
do different things - Info represents file name, more or less, while
Object represents file contents. I see no reason why it would only make
sense for Object to return filename, or why we should "fix" something
that is not broken.
imo the toString should return something which represents the object and
the filename satisfy that better than a "random" line from the file.
but I agree that this isn't a real concern and doesn't need fixing.
--
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
I also agree that we don't need to fix this, nor break BC. It is confusing
as hell but it's there now and changing it would be more disruptive.
Is there a desire from anyone to gracefully throw E_DEPRECATED
in a future
version of PHP 5.x when someone tries to __toString() the SplFileObject but
only get back a single line ?
That's the only "plan forward" I can see feasible if we decided to do
anything, otherwise we should move on.
2013.01.30. 19:16, "Stas Malyshev" smalyshev@sugarcrm.com ezt írta:
Hi!
But this isn't that strong of an argument, and I think that following
what SplFileInfo does would be more sensible (echoing the filename),
but
I'm not sure change would worth breaking BC for.I don't see why it would be more sensible. It's different objects that
do different things - Info represents file name, more or less, while
Object represents file contents. I see no reason why it would only make
sense for Object to return filename, or why we should "fix" something
that is not broken.imo the toString should return something which represents the object and
the filename satisfy that better than a "random" line from the file.
but I agree that this isn't a real concern and doesn't need fixing.--
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
Is there a desire from anyone to gracefully throw
E_DEPRECATED
in a future
version of PHP 5.x when someone tries to __toString() the SplFileObject but
only get back a single line ?
Absolutely not.