News for hackers. Stuff that matters.
Re:Union Types have been accepted for PHP 8 by ashton314 on Saturday November 09, 2019 @05:00PM (#21494689)
> there's no such thing as a "perfect programming language". Every language has it's quirks and that's fine. PHP might be old and boring but it helps you get things done faster than using the new coolest language.

I disagree with you on this point. I worked with PHP for two years on some legacy systems, as well as some Laravel-based systems. While Laravel yields results that are worlds away from PHP build in the 90's, I still wasted dozens of hours because of poor constructs that the language is unfortunately coupled to. I documented each instance where I wasted a significant amount of time due to issues like scoping issues, closure problems, arrays-that-are-arrays-sometimes-but-maps-other-times, etc.

PHP has gotten better. But there are so many good choices that provide much better tooling, guarantees, etc. Examples: Ruby, Go, and Elixir. Heck, even Perl is more sane about its data types in some ways by enforcing consistent comparisons with `==` vs. `eq` and much more robust data structures.

I agree that every language has it's quirks. But I think PHP has so many that it seriously gets in the way often. I don't think it offers any significant velocity gains over using, say, Ruby.

Re:Union Types have been accepted for PHP 8 by uryga on Saturday November 09, 2019 @09:02PM (#21495938)
> arrays-that-are-arrays-sometimes-but-maps-other-times

god, the time i spent going paranoid debugging a stray "undefined array index 0" just to find out

  array_filter(
    is_uppercase,
    ['a', 'B', 'c', 'D']
  )
returns

  [1 => 'B', 3 => 'D']
and not

  ['B', 'D']
like every other `filter(...)` i've used...

EDIT to be fair, i see the rationale – having the index of the filtered element is useful sometimes, and requires some contortions with the usual impl of filter. it's quite neat, because you get both the index and the elements! but it's just... surprising as the default, and PHP's conflation of maps and lists obscures it – all the docs say is "array keys are preserved", which makes sense in retrospect, but doesn't really jump out for something with this much impact

Re:Union Types have been accepted for PHP 8 by ashton314 on Sunday November 10, 2019 @06:30PM (#21500899)
This is exactly the sort of thing that bites. It totally breaks your functional pipelines: now you have to thread everything through `array_values`. Ew ew ew. No thank you.
Re:Union Types have been accepted for PHP 8 by astrodev on Sunday November 10, 2019 @05:35AM (#21497418)
On the other hand, having learned PHP inside-out as my first language, this is perfectly natural to me, and I miss the flexibility of PHP arrays in other languages :)
Re:Union Types have been accepted for PHP 8 by dabernathy89 on Saturday November 09, 2019 @07:22PM (#21495519)
Never heard anyone complain about scoping or closure issues with PHP. I'd be interested to hear more if you recall.
Re:Union Types have been accepted for PHP 8 by jamroom on Saturday November 09, 2019 @08:10PM (#21495690)
I don’t believe there are maps in PHP - just plain arrays which to be honest are pretty simple to use. Unlike say JavaScript where arrays are always numerically indexed, PHP can use any value as an index.
Re:Union Types have been accepted for PHP 8 by azernik on Saturday November 09, 2019 @08:50PM (#21495886)
JavaScript, by contrast, doesn't have arrays - it just has objects, which can have numerical keys.

  a = []
  a.foo = 'bar' // works!

  b = {}
  b[0] = 42 // also works!
I happen to think these designs are both INSANE, but even if you like them, PHP has no advantage here.
Re:Union Types have been accepted for PHP 8 by roblabla on Saturday November 09, 2019 @09:28PM (#21496025)
I’m pretty sure this is wrong. JS objects and arrays are different. The former will only yield elements from the array itself when iterated on, and has a consistent iteration order, while the later does not.
Re:Union Types have been accepted for PHP 8 by azernik on Sunday November 10, 2019 @02:03AM (#21496830)
That's because arrays happen to override the special [Symbol.iterator] method:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

Arrays are objects of class Array which have special literals, override the toString() method, and update the non-enumerable "length" property on certain operations.

Try calling Object.keys([1, 2, 3]), or even better try Object.getOwnPropertyNames([1, 2, 3])

Re:Union Types have been accepted for PHP 8 by TurningCanadian on Sunday November 10, 2019 @12:49AM (#21496618)
Re:Union Types have been accepted for PHP 8 by mncharity on Saturday November 09, 2019 @10:19PM (#21496190)
Re:Union Types have been accepted for PHP 8 by cutler on Sunday November 10, 2019 @01:00AM (#21496657)
JS arrays are array-like objects. That's the official line anyway.
Re:Union Types have been accepted for PHP 8 by uryga on Saturday November 09, 2019 @08:52PM (#21495898)
so isn't it like PHP, which doesn't have lists, only maps with integer keys?
Re:Union Types have been accepted for PHP 8 by azernik on Sunday November 10, 2019 @02:17AM (#21496872)
With the exception that unlike (I think?) PHP, JS also uses maps (w hich can only have string keys [1]) to implement objects and classes. Hence their being called objects, not maps.

[1] So how do arrays have numerical keys? Well... actually they don't. I was trying to make JS look better than it is in my original comment. Actually, array keys are stringified versions of numbers, and values are automatically cast to string when you put them in indexing braces.

  Object.keys([1, 2])
  --> ["0",  "1"]
As you can imagine, this leads to gotchas when you assume that a JS object can have, say, a date as a key

  b = {}; b[new Date()] = 10;
  Object.keys(b)
  --> [
    "Sat Nov 09 2019 23:15:58 GMT-0800 (Pacific Standard Time)"
  ]
Re:Union Types have been accepted for PHP 8 by uryga on Saturday November 09, 2019 @08:50PM (#21495891)
i think that's just a matter of naming – afaik PHP "arrays" are what you'd call a map/dict/hash in other languages
Re:Union Types have been accepted for PHP 8 by capn_cabbage on Saturday November 09, 2019 @10:35PM (#21496234)
I believe it is just a naming issue. Not even sure that PHP devs have agreed on how to name the type. Internally, PHP calls them both arrays and hash tables if I understand correctly.

In Zend/zend_types.h[1] of PHP source:

    typedef struct _zend_array zend_array;
    typedef struct _zend_array HashTable;
    struct _zend_array {
        ...
    };
That being said AFAICT, HashTable and zend_array are used interchangeably throughout the source. I am not a C programmer, but I did write a couple of PHP extensions and that was my general understanding. Perhaps it is a compatibility issue or just used to abstract types differently in various areas of the C API.

Check out [2] for a deeper understanding of how arrays are handled internally in PHP.

[1]: https://github.com/php/php-src/blob/php-7.3.11/Zend/zend_typ...

[2]: https://nikic.github.io/2014/12/22/PHPs-new-hashtable-implem...

Re:Union Types have been accepted for PHP 8 by igouy on Monday November 11, 2019 @12:26PM (#21506665)
rtm ?

"An array in PHP is actually an ordered map."

https://www.php.net/manual/en/language.types.array.php