Java-like Collections in PHP

后端 未结 7 1343
情话喂你
情话喂你 2021-01-04 01:48

I\'m learning PHP5 (last time I checked PHP was in PHP4 days) and I\'m glad to see that PHP5 OO is more Java-alike than the PHP4 one but there\'s still an issue that makes m

7条回答
  •  生来不讨喜
    2021-01-04 02:37

    I, too, was somewhat dismayed to find no Collection type classes in PHP. Arrays have a couple of real disadvantages in my experience.

    First, the number of functions available to manipulate them is somewhat limited. For example, I need to be able to arbitrarily insert and remove items to/from a Collection at a given index position. Doing that with the built-in language functions for arrays in PHP is painful at best.

    Second, as a sort of offshoot of the first point, writing clean, readable code that manipulates arrays at any level of complexity beyond simple push/pop and iterator stuff is difficult at best. I often find that I have to use one array to index and keep track of another array in data-intensive apps I create.

    I prefer working in a framework (my personal choice is NOLOH). There, I have a real Collection class called ArrayList that has functions such as Add, Insert, RemoveAt, RemoveRange and Toggle. I imagine other PHP frameworks address this issue as well.

提交回复
热议问题