Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:104478 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 28930 invoked from network); 20 Feb 2019 20:24:22 -0000 Received: from unknown (HELO mail-vs1-f66.google.com) (209.85.217.66) by pb1.pair.com with SMTP; 20 Feb 2019 20:24:22 -0000 Received: by mail-vs1-f66.google.com with SMTP id u64so14366795vsc.12 for ; Wed, 20 Feb 2019 09:09:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=kzCvaSYBSH1S6wcSYiDTdzSzuVgbV8DSlP5T9NllQzQ=; b=CbCjwgi1vcWV1/zIB6wF70Qb+GlTd/TQpEGiIABAECy/SjgZO4SEAMlfj134b5tA3+ WHeKbo92u6ucSAnLgBqgHcYgKOX1tFVEGUoBe8MH61dGhrp/YU0PSKk3EY11a7ERuCY3 G3ZF/fjfaFtWIt/LyHYLzki3jmyA55DhkCnyV1D0+cXNcObjSQTEjH/+me8iTh7EWCVx 5fOkynFFC4mSUNDJXsNnd0arIb6qpAaEQwu+QrJha5Bf3ImGb7oJCCkDSOkXods4uFy6 DzOndjeY/Jk+vRt2pDmioby2fR6rzbnlBgjUmwlduHooCCO5HLhZviU74fce/iCjDBrb TaZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=kzCvaSYBSH1S6wcSYiDTdzSzuVgbV8DSlP5T9NllQzQ=; b=DJcOF4eDKIyKctqSXrGtzPrCp9mexGwIlUuz+VX4jCtaLxPy2DYZ1IFRisxSOI38u3 mB6ElNYPliWKS422OKSI5+y3hkQM4+3Je+qF7r4Y2NMKQ7XWugZ38zl06m1aa8WljiqL Z2GUHVRKVKG9v4fUZORYIjHJGsPme3POeAOLs+wzx2Yv+y0yqzGEyG/jwu6xNeIJgUdw U/RjOretpb3VElDW6uygvjyDzXG3RpVgT/BoGYCV+kBykstGTHi37Qg2KbZJS4Q1Ba1c 6z4KHi2KNPanAAdNhfmL12YkBMyu0yGj0EIKqYh7kjXAcFdBGVFiMaWD9iS7IXySvetS hIsA== X-Gm-Message-State: AHQUAubX4ZXRjXtQ4hoJz1/eXrIzPxqgTa3VChW2IAC+Kw7dEacgldAw OfCSESPkB95ZTLjiV6KL2OElYJhohOfogFrVdBc= X-Google-Smtp-Source: AHgI3IbZoIa6aVrYsEkeCE9Wu1TblzcKBSqRg5RMlOJfVgaG3sMMucOBqIgHLtc9XVqIbSd99lcGQ8ry85+sDYPCoM4= X-Received: by 2002:a67:874e:: with SMTP id j75mr16338879vsd.12.1550682557462; Wed, 20 Feb 2019 09:09:17 -0800 (PST) MIME-Version: 1.0 References: <3247148.HBVCQ37Dhl@vulcan> <5767621.reugzEP4TJ@vulcan> In-Reply-To: <5767621.reugzEP4TJ@vulcan> Date: Wed, 20 Feb 2019 18:09:05 +0100 Message-ID: To: Larry Garfield Cc: PHP internals Content-Type: multipart/alternative; boundary="000000000000ff3c870582566aa8" Subject: Re: [PHP-DEV] Allows arrays to be type-hinted with interfaces mimicking Array behaviour From: george.banyard@gmail.com (Girgias) --000000000000ff3c870582566aa8 Content-Type: text/plain; charset="UTF-8" On Tue, 19 Feb 2019 at 17:33, Larry Garfield wrote: > On Tuesday, February 19, 2019 3:38:16 AM CST Rowan Collins wrote: > > On Tue, 19 Feb 2019 at 02:44, Larry Garfield > wrote: > > > Yes, a lot of existing code uses arrays as anonymous structs. Such > code > > > is, > > > IMO, "doing it wrong". That includes PHP default behavior in many > cases. > > > The > > > super-globals are one of the exceptions, as since by definition you > don't > > > know > > > what the keys could be in advance you need some sort of generic > accessor. > > > > > > superglobals anyway on the grounds that globals are evil (fact check: > > > true). > > > There is often an object wrapping around the session that has get() and > > > set() > > > object be kind-of-array-ish-for-some-part-of-array-ish-ness is a > > > long-standing > > > problem, no question. iterable was a good addition. I think there was > > > discussion of a countable type hint at one point, but I don't think it > > > made it > > > That leaves ArrayAccessible (aka, you can put [] after it and something > > > useful > > > happens), which is what you describe. I would not be against adding > such > > > a > > > instead of an object is Just Plain Wrong(tm). Even a bare public > property > > > class is superior in every possible way, before we even get into > questions > > > of > > > useful accessor methods. > > > > > > be. (Eg, parsing JSON, GET or POST parameters, etc.) > > > 2) You're being sloppy and not thinking through your API. > > > struct into an object that has appropriate accessors that can then > handle > > > error cases, missing values, etc. Not doing so results in things like, > > > say, > > > particular anonymous-struct was missing a certain key, which I didn't > know > > > was > > > possible, but it broke the whole application. (Seriously, that was my > > > entire > > > struct, even if you're using __get() or ArrayAccerss to do it. > (SimpleXML > > > is > > > an example of that approach, for better or worse.) > > > where you have to care, but those are edge-casey. > > > > > > ArrayAccessible is a code smell, and the solution is to properly > structure > > > your data and type hint on a defined class instead. Including an > > > ArrayAccessible pseudo-type would be just a matter of completeness, not > > > something I would advise anyone actually use. > > > > Hi Larry, > > > > The above post seems to have been mangled somewhere and lost some of its > > text. It's mostly readable anyway (and I think I agree with what's there) > > but if you have a copy of the original anywhere, you might want to > re-post > > it. > > > > Regards, > > No, I don't. That's the second time KMail has eaten > half > a message on me. I am now officially in the market for a new local mail > client, because that's JUST NOT OK, KMAIL! > > The gist is that if you're dealing with unknown foreign data (JSON, > GET/POST, > etc.) you should be wrapping the raw structure into an object that > provides a > better interface, default value handling, error handling, etc. If you > know > what the keys are in advance, make it an explicit class, period. > > So while I would be OK with an ArrayAccessible type hint for completeness, > I > would view using it as a code smell. > > Insert debate about union types here, as it's bound to come up at this > point. > > Excuse me while I go find a new Linux mail client that isn't a buggy data- > losing . > > --Larry Garfield In all honesty the idea/proposal was more for completeness than anything else. I do think I can agree with you that ArrayAccess is *probably* a code smell. But my initial email was more to see if this would get any support and secondly to see how to implement this. Because as said using pseudo types are (from my understanding) easy to implement but could possibly limit the scope of new Array like interfaces (not sure if it is practical at all but as an idea Sortable and/or Comparable, even tho comparable probably needs a whole pseudo type on its own to accepts strings, integers floats and what's not) whereas some other possible (from my limited understanding) could be more bullet proof to future change. But I suppose one question is if PHP want's to continue to encourage usage of the ArrayAccess (and other array like interfaces) for objects. I could well see an argument for a ``countable`` pseudo type if decided ArrayAccess as an interface "should" be scrapped as you say leads to code smell. Anyway thanks again for the feedback. Best regards George P. Banyard --000000000000ff3c870582566aa8--