Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:69510 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 80093 invoked from network); 7 Oct 2013 20:28:10 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 7 Oct 2013 20:28:10 -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 209.85.212.175 as permitted sender) X-PHP-List-Original-Sender: rowan.collins@gmail.com X-Host-Fingerprint: 209.85.212.175 mail-wi0-f175.google.com Received: from [209.85.212.175] ([209.85.212.175:46526] helo=mail-wi0-f175.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id E1/A0-09409-95913525 for ; Mon, 07 Oct 2013 16:28:10 -0400 Received: by mail-wi0-f175.google.com with SMTP id ez12so5479548wid.2 for ; Mon, 07 Oct 2013 13:28:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type; bh=bIv68ZTpRaGqNEpi7qrgk8AJM9azC7Z0Hzi7ekhxGvM=; b=S8lgGYXBB53+4bSp2WCtGujV0rs4YlEt4Zxq651ndBF29yrMSezOorlFB8TDsyLGAc EpvkVLMm03sWXhSACaLwGeWP1XcrKF9uV1TnO7noahUz1xyCRP+EI0goVrHQRfnCChIa JZhzYEbBLl2WiR6aEN1RrIIHM6yZPoIyld/hPjeXiwif/ajZMGJyzWZw/BXAVUagYXFs o81sXOrIPDs184v0eH/d+T8ShRoJQXFiXILYkq8vsukoR1LKURkp25u8xR2oL8QbvXvB UEpEJpDFj1LLttpf98XJy0JmQl16yCDmJgvuP2B1KSXnathdva8NLyse2wDuEaq0AkG9 P7SQ== X-Received: by 10.180.73.109 with SMTP id k13mr20632482wiv.35.1381177686182; Mon, 07 Oct 2013 13:28:06 -0700 (PDT) Received: from [192.168.0.2] (cpc19-brig17-2-0-cust25.3-3.cable.virginmedia.com. [81.101.201.26]) by mx.google.com with ESMTPSA id c13sm386513wib.5.1969.12.31.16.00.00 (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 07 Oct 2013 13:28:05 -0700 (PDT) Message-ID: <52531955.1010804@gmail.com> Date: Mon, 07 Oct 2013 21:28:05 +0100 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130801 Thunderbird/17.0.8 MIME-Version: 1.0 To: "internals@lists.php.net" References: In-Reply-To: Content-Type: multipart/alternative; boundary="------------070605080801020405010403" Subject: Re: HTTP supergloblas and request body/query (was: Parsing PUT data) From: rowan.collins@gmail.com (Rowan Collins) --------------070605080801020405010403 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit On 07/10/2013 18:19, Daniel Lowrey wrote: > >> You can't efficiently model an HTTP request with associative > arrays. Period. > > > The fact is that for 99% of use cases, yes you can, and developers > > happily do so. > > Leaky abstraction is leaky. If this is truly an efficient model of the > HTTP request then why do we fragment it out into $_SERVER and > $_COOKIES and $_FILES and $_POST and $_GET and php://input? I don't > know what your definition of "efficiently model" is, but it must be > different from mine. Array Oriented Programming !== design. Ah, OK, I was only really talking about $_POST and $_GET, since they were the topic of this thread. I'm not quite sure why breaking different aspects of the HTTP request into different interfaces is fundamentally a problem, but I agree that you can't model /all/ aspects of an HTTP request as associative arrays. To go through the relevant superglobals in turn: $_GET: The param=value&... format for query strings is so universal, whether or not generated by a form, that it can largely be taken for granted. Within that format, the only thing that can't be handled as a hash is a repeated key; PHP takes the approach that foo[]=bar always creates an array, and foo=bar never does, with later values "winning". This covers 99% of what people need to do with query strings. The mutability and globalness aren't great, but any method ->getQueryStringParam('foo') would be indistinguishable from ->getQueryStringHash()['foo'] $_POST: Deals with the two generally accepted form encodings for POST requests, in a way that matches $_GET, but while allowing programmers to distinguish the two rather than clobbering thm into one array. $_COOKIE: Again, the structure of the Cookie: header in PHP is fundamentally a set of name=value pairs, making a hash a perfectly reasonable structure. The asymmetry with set_cookie() is unfortunate, although some asymmetry is inevitable given the underlying headers. At least it's better than the abomination that is document.cookie :P $_REQUEST: This is an unnecessary bit of redundancy, although it reminds me that if you do want to merge query string and posted data, having them as hashes is very handy. $_FILES: A bit awkward. I can guess the argument for splitting it from $_POST, but it's crying out for a more OO representation of the individual entries. They're still name-value pairs though. $_SERVER: This is the only one that really doesn't work at all. I was going to mention it earlier, but didn't want to drift off-topic in my earlier messages. It's an awful jumble of HTTP headers, PHP-specific data, and arbitrary environment variables which happen to have come through from the SAPI. It contains the requested URL in various parts with inconsistent names, and I refuse to go near it without a sane wrapper class. HTTP headers are fundamentally key-value pairs, although they can repeat, so more like key -> array of values. Environment variables are key-value too. The rest of it, along with file_get_contents('php://input'), is odds and ends that need a separate abstraction. Well, that's the way I see it anyway. The superglobals themselves aren't great, but outside of APIs (which will generally involve a bit of framework-y-ness anyway) the associative array interface is what most people will end up wanting anyway. Regards, -- Rowan Collins [IMSoP] --------------070605080801020405010403--