# echo new SplFileObject(__FILE__);

- Thread: https://externals.io/message/65442
- Original archive: https://news-web.php.net/php.internals/65442
- Started by hakre on 2013-01-29T22:29:42+00:00
- Messages: 13
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #65442 — hakre — 2013-01-29T22:29:42+00:00

URL: https://externals.io/message/65442#65442

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

---

## #65444 — Ferenc Kovacs — 2013-01-29T22:51:07+00:00

URL: https://externals.io/message/65442#65444 · In reply to: #65442

> 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

---

## #65447 — Charles PHP — 2013-01-29T23:00:27+00:00

URL: https://externals.io/message/65442#65447 · In reply to: #65444

This feels like a bug to me. Why would SplFileObject::__toString return the
current line while SplFileInfo::__toString returns file path?




> 
>
> > […]
> 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

---

## #65448 — Stas Malyshev — 2013-01-29T23:00:39+00:00

URL: https://externals.io/message/65442#65448 · In reply to: #65442

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

---

## #65476 — Ferenc Kovacs — 2013-01-30T07:42:52+00:00

URL: https://externals.io/message/65442#65476 · In reply to: #65448

2013.01.30. 0:01, "Stas Malyshev" <smalyshev@sugarcrm.com> ezt írta:
>
> Hi!
>
> > […]
>
> __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
>
> --
> 
> 
>

I've changed the docs so the __toString is listed in the method synapsis,
see http://docs.php.net/manual/en/class.splfileobject.php

---

## #65506 — hakre — 2013-01-30T15:44:15+00:00

URL: https://externals.io/message/65442#65506 · In reply to: #65448

----- 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.php
> 
> it'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

---

## #65508 — Paul Dragoonis — 2013-01-30T15:54:52+00:00

URL: https://externals.io/message/65442#65508 · In reply to: #65506

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 -----
>
> > […]
>
> 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

---

## #65509 — Ferenc Kovacs — 2013-01-30T15:57:23+00:00

URL: https://externals.io/message/65442#65509 · In reply to: #65506

>
>
> ----- Ursprüngliche Message -----
>
> > […]
>
> 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

---

## #65511 — hakre — 2013-01-30T16:17:21+00:00

URL: https://externals.io/message/65442#65511 · In reply to: #65508

> 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

---

## #65514 — Stas Malyshev — 2013-01-30T18:16:39+00:00

URL: https://externals.io/message/65442#65514 · In reply to: #65509

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

---

## #65517 — Ferenc Kovacs — 2013-01-30T18:44:10+00:00

URL: https://externals.io/message/65442#65517 · In reply to: #65514

2013.01.30. 19:16, "Stas Malyshev" <smalyshev@sugarcrm.com> ezt írta:
>
> Hi!
>
> > […]
>
> 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

---

## #65518 — Paul Dragoonis — 2013-01-30T18:48:14+00:00

URL: https://externals.io/message/65442#65518 · In reply to: #65517

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:
> > […]
> but
> > […]
>
> 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.
>
> > […]

---

## #65519 — Peter Cowburn — 2013-01-30T19:07:23+00:00

URL: https://externals.io/message/65442#65519 · In reply to: #65518

> 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.
