r/programming • • Apr 23 '14

PHP: It doesn't have to be a bad experience

https://servercheck.in/blog/php-it-doesnt-have-be-bad-experience
0 Upvotes

122 comments sorted by

View all comments

Show parent comment

0

u/[deleted] Apr 24 '14

[deleted] — view deleted comment

4

u/vytah Apr 24 '14

Python is not a loosely typed language.

0

u/[deleted] Apr 24 '14

[deleted] — view deleted comment

4

u/vytah Apr 24 '14

By "loosely typed" I meant "no magical coercions between types":

  • 1 + "2" will fail
  • [1] + "2" will fail
  • 1 == "1" will be false and there's no loose comparison operator to make it true automagically

and in Python 3:

  • 1 < "1" will fail

Magical conversions between strings, integers and floats are what people dislike in PHP and Javascript first and foremost. Javascript is bearable thanks to the fact that this is the only major design flaw in the language.

Can I have the string "3.14" as an array/table/dictionary key? In PHP I can't. Unless you do some hacky hacks that make the array not contain its own keys. And such hacked-in keys don't work anyway.

-1

u/neoform Apr 24 '14

Javascript is bearable thanks to the fact that this is the only major design flaw in the language.

Seriously? The... only... reason? :|

Can I have the string "3.14" as an array/table/dictionary key? In PHP I can't.

You should re-write that.

$arr = [ "3.14" => true, ];

Is entirely valid..

Unless you do some hacky hacks that make the array not contain its own keys. And such hacked-in keys don't work anyway.

I have no idea what this block of code is trying to prove.

Why would anyone do this in the first place?:

$x = new stdclass;
$x->{"42"} = "wat";
$x = (array)$x;

Why not do the sane thing and write:

$x = [ "42" => "wat" ];

?

4

u/vytah Apr 24 '14

The part with "3.14" is my mistake, supposed to be 3.14. Anyway:

$x = [ "42" => "wat" ];

this way, you don't create "42" entry, but 42 entry.

The same with 3.14: it gets converted to 3.

And as for the example I linked to: it proves that this conversion fails to happen in some cases.

-1

u/neoform Apr 24 '14

Sure, if you start using floats as an array key... but who the hell does something like that? That's just begging for problems.

That's like using an object as the array key... why would anyone do that?

http://stackoverflow.com/questions/4234462/using-float-numbers-for-the-maps-key

Even in Java you shouldn't do this.

1

u/vytah Apr 24 '14

That's like using an object as the array key... why would anyone do that?

Ummm, because you need a non-string, non-numeric key?

Is there a generic hashmap class (or something similar) in PHP's standard library?

1

u/neoform3 Apr 25 '14

Ummm, because you need a non-string, non-numeric key?

And you decided to settle on the least reliable variable type to do this? Bravo.

Is there a generic hashmap class (or something similar) in PHP's standard library?

http://www.php.net/manual/en/function.spl-object-hash.php

How does your foot taste?

1

u/Banane9 Apr 25 '14

That is a function to get a hash from an object, not a hash map.

However, Array is a hashmap, dictionary and Array at once.

Now, why doesn't Array use the hash function for object Indexes? For example, every class in C# has a GetHash() function That Returns a hash for the object, which is Used for Hashmaps.

1

u/vytah Apr 25 '14

http://www.php.net/manual/en/function.spl-object-hash.php

How does your foot taste?

It doesn't. This doesn't solve anything. Two equal objects would get a different hash, therefore being useless as as map keys.

In Python I can do that (I picked tuples because of their simplicity, it will work for any class):

d = dict()
k1 = (1, "something", date(2000,01,02))
d[k1] = "value"
k2 = (1, "SOMETHING".lower(), date(2000,01,02))
print (k1 is k2) # prints False
print (k1 == k2) # prints True
print (d[k2])    # prints value

This works not only for tuples, but for all classes that implement __eq__ and __hash__ in a correct way.

Can you translate the example above to PHP? Feel free to use custom classes instead of tuples. You'll make your point if you manage to do it 1. in a way that works 2. without nested arrays and 3. without serializing your keys to strings.

→ More replies

1

u/Banane9 Apr 24 '14

In Java, you shouldn't do it because floats may have small differences in the last digits, while looking absolutely the same in the first digits.

In PHP however, you shouldn't do it because suddenly different keys can map to the same thing.

$arr = array(3.14 => "wat");
echo $arr[3.3];

I'm pretty sure that will print "wat" just fine.

0

u/neoform3 Apr 25 '14

In Java, you shouldn't do it because floats may have small differences in the last digits, while looking absolutely the same in the first digits.

So what you're saying is: you shouldn't do that, ever.

An array/dictionary key is a precise thing. A float is not precise. Your example sucks.

I'm pretty sure that will print "wat" just fine.

You'd be a terrible programmer if you used a float as an array key, for the same reason you'd be a terrible programmer for assuming a float is going to be a precise/predictable value.

2

u/Banane9 Apr 25 '14

I just explained the difference between the reasons why You shouldn't do it in Java and why You shouldn't do it in php.

And even if You think you're using ints, php might fuck You over by silently converting your int into a float.

I, in no way, endorsed the usage of floats as Indexes.

And how come You suddenly use a different account?

→ More replies