On Mon, 18 Feb 2019 at 17:26, Larry Garfield <la...@garfieldtech.com> wrote:

> On Sunday, February 17, 2019 9:24:05 AM CST Girgias wrote:
> > Greetings internals,
> >
> > I would like to have your opinion on being able to type hint arrays and
> > objects which implement
> > array-like interfaces (notably ArrayAccess, Countable, and Iterator) as
> > currently if a function /method only needs one specific feature of an
> array
> > and is willing to accept array-like objects.
> > There are some rather repetitive boilerplate and the impossibility of
> using
> > type hints.
> >
> > An example of such a function, (where $arrayLike could be custom session
> > object) :
> >
> > /** ArrayAccess|array $arrayLike */
> > public function checkCSRF($session) {
> >    if (!is_array($session) && !($session instanceof ArrayAccess)) {
> >        throw new Exception();
> >    }
> >    // Check token
> >    if (!isset($session['CSRF']) {
> >        throw new Exception();
> >    }
> >    // Do some more stuff ...
> > }
> >
> > As it can be seen this function/method doesn't need any of the other
> > properties of an array
> > such as Countability or Traversability to do its job.
> > However, in the current state of PHP, it is impossible to accept
> array-like
> > objects without
> > writing the initial if statement.
>
>
> While I would like to see better consistency between arrays and array-ish
> objects in principle, in this case I think it's the wrong approach.
> You're
> using an associative array as a cheap anonymous struct.  Don't do that.
> It's
> far less self-documenting, far less type safe, far more error prone, and
> far
> less performant.  Just switching $session from an array-ish value to a
> class
> with public properties would make it use half as much memory.  cf:
>
> https://steemit.com/php/@crell/php-use-associative-arrays-basically-never
> https://steemit.com/php/@crell/php-never-type-hint-on-arrays
>
> Making it easier to use
> anonymous-array-or-object-acting-like-anonymous-array
> would be a step backwards in every possible way.
>
> --Larry Garfield


Hello Larry,

First of all thanks for the feedback and link to your benchmark of Objects
vs Arrays.

However, I'm not exactly sure what your point is. Maybe I'm not
understanding or
maybe I didn't express myself clearly enough.
But I don't see how what you say contradicts me.

I do totally agree with you that Classes should be used most of the time,
however my take from your article is that you only encourage the use of
``iterable``
instead of ``array`` which I 100% agree on.
However it is impossible to type-hint an argument with Countable or
ArrayAccess
such that it accepts object but, key point, *ALSO* accepts arrays, even if
from what I
understand you want to limit its usage to the bare minimum.

Also you can disagree with the fact of using an array as a "cheap"
anonymous structure
but, by default, PHP uses arrays like that for get, post, sessions, files,
cookies, server,
request, and environment variables. So if I want to use an object, which
implements
ArrayAccess, to represent this data because it is more structured and
memory efficient
but can't pass it to a function (/method/constructor) from a library
because it typehints
against array (and yes I saw that you discourage the use of array
type-hinting but this
only works for iterables not if someone accesses it like an array or wants
to count it)
that for example validates my POST values.
However I don't expect a library to just accept an object because more
likely than not
it expect the raw data to be an array even if it doesn't care about the
other "features"
of an array.
And until PHP uses objects for all superglobals I don't see an easy way
around it.

Now in the case I wasn't clear what I would like is that IF there is a
type-hint for ArrayAccess
and Countable, arrays are automatically accepted and the if statement can
be removed.

Now if you are saying object shouldn't implement ArrayAccess and methods
should just type hint
against that object that's a whole other issue, but I don't think this is
what you are trying to get at.

So I would love if you could clarify what you meant as you seem to have
some insight/opinion which
seems valuable and maybe also give your opinion on how you would like to
see better consistency
with arrays and array-like objects.


Best regards

George P. Banyard

Reply via email to