Is this really by design?

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 880 Views - Last Post: 30 April 2017 - 06:45 PM

#16 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 879
  • View blog
  • Posts: 1,893
  • Joined: 30-January 09

Re: Is this really by design?

Posted 30 April 2017 - 05:15 PM

I'll weigh in here, because I've had to deal with (read: avoid) arrays in PHP recently while writing my framework.

In writing my framework, I've opted to avoid native PHP arrays altogether except where it is a necessary transitionary data type from some external source (e.g. DB result fetched as array, $_POST), in which case they are immediately converted to objects to ensure validation and type safety. It is possible to move away from native PHP arrays - they are poisonous, it has to be admitted. Never in a million years should an array be a native type. An array is a structure, not a type. IMO, native arrays should be treated with the same contempt as mysql_* functions and directly injected variable values in SQL strings.

So, what is the alternative?

There's only one as far as I can see, and that is PHP ArrayObjects. Once you start looking at arrays as iterative objects, as collections of specific types (whether native or object), rather than some hybrid native "thing" that can take anything as it's values, have no/1/2 key(s), then you can start thinking about arrays in an OO context.

This might bristle some of my colleagues here, but come on, PHP arrays are a disaster. If you want a tree of various types, use JSON or XML. If you want a collection of one type (object or native), use an ArrayObject and enforce that type on __construct() or append (you won't be surprised that my biggest gripe with PHP is the lack of typed collections). It may be extra work beyond the perceived simplicity of dynamic and very loose typing, but it's about data integrity and ultimately data/type safety. If a database randomly returned NULL, 0, "", "{}", or "[]" when you polled a boolean field, you wouldn't use it - treat your data with the same respect in PHP too.
Was This Post Helpful? 0
  • +
  • -

#17 jon.kiparsky  Icon User is offline

  • Screw Trump (before he screws you)
  • member icon


Reputation: 10624
  • View blog
  • Posts: 18,180
  • Joined: 19-March 11

Re: Is this really by design?

Posted 30 April 2017 - 06:27 PM

Quote

Never in a million years should an array be a native type.


I agree with you that the PHP array is fundamentally demented, but this seems like a pretty strong statement if you mean it to be taken generally. I can hardly think of a language that I've used since my first experiments with BASIC that doesn't have a native array type of some sort, often more than one, and in general I find little to complain about. (the only two that come to mind are Scheme and Prolog, which generally prefer immutable data structures)

View PostCTphpnwb, on 28 April 2017 - 02:32 PM, said:

Yes, I'm sure. What's broken is the default setting (and the lack of acknowledgement/warning in the docs), but if you use strict mode it will function as it should. If you can show a case where it doesn't, then I'd agree that the function is totally broken.


So it doesn't bother you that the default behavior of this function has to change so radically from the default used everywhere else in the language? Or are there other functions which should change in this way as well?
Was This Post Helpful? 0
  • +
  • -

#18 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 879
  • View blog
  • Posts: 1,893
  • Joined: 30-January 09

Re: Is this really by design?

Posted 30 April 2017 - 06:45 PM

View Postjon.kiparsky, on 01 May 2017 - 12:27 PM, said:

Quote

Never in a million years should an array be a native type.


I agree with you that the PHP array is fundamentally demented, but this seems like a pretty strong statement if you mean it to be taken generally. I can hardly think of a language that I've used since my first experiments with BASIC that doesn't have a native array type of some sort, often more than one, and in general I find little to complain about. (the only two that come to mind are Scheme and Prolog, which generally prefer immutable data structures)

I'm pro-"keyed typed Collection", and this is probably a bit of a hardline stance, but I don't think there is a legitimate reason to have a collection that isn't strongly typed against either a native type or an object. If you start working with a collection, and you iterate through it not knowing a} what types you're getting and/or b} what keys you're expecting, then there's a problem. Eventually you, or someone who inherits your code, will get caught in a horrible trap where you have to breakpoint to know what the data schema is. That's critically bad IMO.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2