Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:79131 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 84153 invoked from network); 24 Nov 2014 19:14:42 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 24 Nov 2014 19:14:42 -0000 Authentication-Results: pb1.pair.com smtp.mail=rowan.collins@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=rowan.collins@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.45 as permitted sender) X-PHP-List-Original-Sender: rowan.collins@gmail.com X-Host-Fingerprint: 74.125.82.45 mail-wg0-f45.google.com Received: from [74.125.82.45] ([74.125.82.45:61850] helo=mail-wg0-f45.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 84/80-13333-1A383745 for ; Mon, 24 Nov 2014 14:14:42 -0500 Received: by mail-wg0-f45.google.com with SMTP id b13so13252603wgh.18 for ; Mon, 24 Nov 2014 11:14:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=user-agent:in-reply-to:references:mime-version :content-transfer-encoding:content-type:subject:from:date:to :message-id; bh=pv9daL2Ha3if9UtjHua6kB56+x8ZhwG5Hf0REVdF8zo=; b=vM8UAgCUFDR8eTDjL3kGaOxKHAus9K+ZuxR0A6ka2nuQqE7Qbgj58irCzPIphWdbfi Pp+DvsfDJyHmeFgqJaF+Fk80QYr7PNQ4Z5ZGSH3BfjCf8gxkZrJGmoU3hDRPL5QD1S06 1A7/neIQHTGyfR2BU7AcN7mdP6I6gap6a1yO00QpY5NMZF1UuJOfGBwUD34ASzvrWODr xrkivtE7lghaGwuVXRznPMtvjeeV6qWRugBvNs6bCoP44e/8HbPQ0hpCA/1DZFTe1G0o vjZ2BZP97waG3vfuYD4I6wUrE00HYSqIF16k9jNuOgiu0jhU8XfOLwmved1Rh9rHIZcL NfKQ== X-Received: by 10.180.205.196 with SMTP id li4mr25150109wic.63.1416856479151; Mon, 24 Nov 2014 11:14:39 -0800 (PST) Received: from [192.168.0.3] (cpc68956-brig15-2-0-cust215.3-3.cable.virginm.net. [82.6.24.216]) by mx.google.com with ESMTPSA id ge17sm13302961wic.0.2014.11.24.11.14.37 for (version=TLSv1.2 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 24 Nov 2014 11:14:38 -0800 (PST) User-Agent: K-9 Mail for Android In-Reply-To: <018601d00776$50681c40$f13854c0$@devtemple.com> References: <012701d0074c$ccfe6f40$66fb4dc0$@devtemple.com> <7E8EF92A-6071-4BDA-ADD5-2C2B54AA2DCB@gmail.com> <018601d00776$50681c40$f13854c0$@devtemple.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=UTF-8 Date: Mon, 24 Nov 2014 19:14:34 +0000 To: internals@lists.php.net Message-ID: <37158DB6-5CFA-4B64-944A-AAF78BDA4566@gmail.com> Subject: RE: [PHP-DEV] enhance fget to accept a callback From: rowan.collins@gmail.com (Rowan Collins) On 23 November 2014 23:36:30 GMT, Bill Salak wrote: >The callback would be given the string as returned by fgets today. The >functional equivalent to fgetjson today is handled by something like >$handle = fopen(~some file~, 'r'); >while (($data = fgets($handle)) !== FALSE) { > $data = json_decode($data, true); > ...other stuff... >} >and would change to >$handle = fopen(~some file~, 'r'); >$decode = json_decode($data, true); >while (($data = fgets($handle,0,$decode)) !== FALSE) { > ...other stuff... >} Since you need a function reference for the callback, you'd actually need a closure to capture the options: $decode = function($data) { return json_decode($data, true); }; This is actually more effort and code than the existing version, so I'm not sure what is gained. Either way, the likelihood is you'd want to wrap this into a user function. As I mentioned earlier, making it into an Iterator is often useful, and potentially as simple as a generator function a bit like this: function fjsoniterator($fh) { if ( ! feof($fh) ) { yield json_decode(fgets($fh), true); } } $fh = fopen(...); foreach ( fjsoniterator($fh) as $data ) { ... } Regards, -- Rowan Collins [IMSoP]