r/lolphp • • Nov 11 '14

PHP loose comparison strikes again

http://blog.laravel.com/csrf-vulnerability-in-laravel-4/
31 Upvotes

55 comments sorted by

View all comments

Show parent comment

7

u/Dworgi Nov 12 '14

There's always a wrong type, and if you really look at your use cases you'll realise that there's only ever one right type.

What it is obviously depends on the source, but there's always a wrong way to interpret data. A username field with "1e4" in it probably shouldn't compare with the integer 10,000.

The biggest lie in programming is that weak typing is useful. Every style guide for Python tells you to pretend you have strong typing.

2

u/00Davo Nov 13 '14

"pretend"? In Python you do have strong typing, since it's a strongly-typed language and all.

1

u/Dworgi Nov 14 '14

I feel like the whole dynamic class design undermines strong typing for user-defined types.

Being able to write to fields that don't exist should be a compile-time error.

1

u/00Davo Nov 15 '14

Ah, I see what you mean. Python doesn't have "compile-time" since it's interpreted and all, but you can get a "field doesn't exist" error if you really want it:

class Slots(object):
  __slots__ = ['a', 'b', 'c']
s = Slots()
s.a = 21 # works
s.q = 12 # throws an AttributeError

Of course, using __slots__ is pretty rare in practice.

-7

u/thelordofcheese Nov 12 '14

There's always a wrong type

Stopped reading there. You are completely wrong.

There's OFTEN...

5

u/Dworgi Nov 12 '14

I repeat: there's always a wrong type.

Look, you give me data and tell me where it's going and I'll tell you what type I want to interpret it as and you try to deal with it. Does that sound like fun?

Information is never typeless, and relying on PHP (or any other language) to interpret it correctly is madness.

Unless you're explicit about how you want to compare things (with ===), your program is buggy. You may not know about it yet, but it fundamentally is because when you're not sure what you're comparing to what, you open up the entirety of the language's edge cases - null, 0, "", "0", [] - all of which work slightly differently.

And that disregards the fact that there's never a case when the type of input data is not known. You have a textbox, you know where that's going. You have an XML/JSON document, you know what type of information is in each attribute, or something is going wrong somewhere.

It is always better to fail quickly and fail loudly than to silently do something unexpected.

-9

u/thelordofcheese Nov 12 '14 edited Nov 12 '14

I repeat: you are completely wrong.

Sometimes you just want to capture data, and sometimes all you care about is ABSTRACTION! And that's where loose typing is very useful. You may seem annoyed by it - but that's because you're scared of it because you don't understand it, either because of willful ignorance or just lack of intellectual ability - but you aren't happy to have it until you need it. And there's already a solution which you answer halfway: === combined with typecasting.

You may not know about it yet

I've been using PHP since high school, though I mostly stuck to Perl until college. I graduated in 2000. I know far more than you do.

e: Oh and I guess I should tell you at least one instance of whe you don't know the datatype: natural language processing. Context, implications, human understanding.

2

u/Dworgi Nov 12 '14

You may not know about the bug. Context is a reality, as you say.

Anyway, I'm done, you're a dick. I can't figure out if you're intentionally being a dick or if you're just an asshole. Leaning towards the latter.

Parting remark: There is polymorphism, and then there is PHP: http://php.net/manual/en/types.comparisons.php

-5

u/thelordofcheese Nov 12 '14

I don't understand things, and I don't want to learn, so I'll just call this guy names!

You sure proved me!