Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:65657 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 83733 invoked from network); 5 Feb 2013 01:52:00 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 5 Feb 2013 01:52:00 -0000 Authentication-Results: pb1.pair.com smtp.mail=morrison.levi@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=morrison.levi@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.214.169 as permitted sender) X-PHP-List-Original-Sender: morrison.levi@gmail.com X-Host-Fingerprint: 209.85.214.169 mail-ob0-f169.google.com Received: from [209.85.214.169] ([209.85.214.169:58856] helo=mail-ob0-f169.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 36/C2-63819-EB560115 for ; Mon, 04 Feb 2013 20:51:59 -0500 Received: by mail-ob0-f169.google.com with SMTP id ta14so7151666obb.0 for ; Mon, 04 Feb 2013 17:51:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:cc:content-type; bh=WDFVIl4NjXma4kSm+Sb5acyACGIkQZJpV3dWY6FfF7o=; b=rlu53aqTT7VYQDHQC7HbqnMsFOmpugzUyFYZzy6ivly0uLQHBwe5Fy57dar1xP1twm 1RmehJsL2xAdEmptpqrMB35qh4rSRkJaovaXiOB9wB17xFqQjL8kCSFV5EDvQC+tHanT +GhgVWJ7QSC1iChfzf8yQpCIOr+hnldFL3Qvei6YNWV/hq8krKStS3gqZ/xCvtcNmvI7 LMOHEFnFn0uIdMtJkD3BQ8V1Z0aknmJ4f88ZWGWXcLnwrDWRCgux3kLNTPINS3hUyKpj +z/+Tf0u4sVy9jf2oH9h9IFggJylGRnLh7u6NZ5cp51WifIF+7Bu6RNaQLFdkM9XCOa3 IGmA== MIME-Version: 1.0 X-Received: by 10.182.245.33 with SMTP id xl1mr6408298obc.91.1360029115850; Mon, 04 Feb 2013 17:51:55 -0800 (PST) Received: by 10.76.96.236 with HTTP; Mon, 4 Feb 2013 17:51:55 -0800 (PST) In-Reply-To: References: Date: Mon, 4 Feb 2013 18:51:55 -0700 Message-ID: To: Charles Sprayberry Cc: "internals@lists.php.net" Content-Type: text/plain; charset=ISO-8859-1 Subject: Re: [PHP-DEV] SplFileObject::__toString potentially returns false From: morrison.levi@gmail.com (Levi Morrison) On Mon, Feb 4, 2013 at 5:56 PM, Charles Sprayberry wrote: > There was a previous discussion on the return value of > SplFileObject::__toString being > an alias to SplFileObject::current() but that was about the design decision > why and not > really suggesting any kind of change or a technical error. In the > discussion it was > established that: > > (1) Changing this would be a BC break > (2) It isn't really a bug in the codebase, the docs were just wrong > > The current implementation has a bug in that current() can potentially > return false, if > false is returned from the __toString() magic method a catchable fatal > error occurs. > > Code ran on 5.4.11 *nix, Apache 2.2 > > --TEST-- > Ensuring we get appropriate output for SplFileObject::__toString > --FILE-- > > $file = fopen('file.txt', 'w'); > fwrite($file, 'A'); > fclose($file); > > $FileObject = new SplFileObject('file.txt'); > foreach ($FileObject as $line) { > echo $FileObject; > } > > echo $FileObject; > ?> > --EXPECTF-- > A > > > --------- > Diff > > 002+ Catchable fatal error: Method SplFileObject::__toString() must return > a string value > in /path/to/splfileobject_tostring.php on line 12 > > --------- > > I would say that SplFileObject:__toString() being an alias to > SplFileObject::current() > makes the use cases for the method very limited and that the current > implementation is > mistaken in returning current(). I can think of no use case where > SplFileObject::__toString() > should return this value. If need to get the current line you're probably > iterating and in > that case should be using foreach() and don't really need to manually take > care of that. > Even if there is a use case for manually invoking > `SplFileObject::current()` you probably > aren't casting a string in that situation and a more expressive method call > is better. > > (1) Leave the implementation the way it is and simply return a blank string > when > current() would return false > > (2) Change the implementation to return SplFileInfo::__toString() > > (3) Change the implementation to return entire file contents > > (4) Return some other unthought of string but remains consistent and logical > > I am personally partial to (2) or (3) perhaps in 5.5 but (1) could probably > happen > sooner? Thoughts? I don't really use this particular object, but from a distance I think something should change. Returning false seems like a bug or design flaw. I can probably be persuaded for any of options 1-3 though I prefer them in 2, 1, 3 order. Looking it up in the helly docs (http://www.php.net/~helly/php/ext/spl/classSplFileObject.html#a79319d744507f828a752981e6f63bf15) I see that this was always the intended behavior, though I don't know why.