Live data from Hacker News

These are things in PHP which make me sad

phpsadness.com

111–120 of 217 posts

Re: These are things in PHP which make me sad

#111
post #34

Earlier quoted context omitted.

It doesn't make sense unless you understand how PHP works - at which point you'd use ===.

> It doesn't make sense unless you understand how PHP works ... In other words: equality is not intuitive. Awesome! --- Let's take a look at "how PHP works": 0 == 08 // true! WTF, really? Well, let's just use the magical 'I-mean-actual-equality-not-some-other-kind-of-equality' operator: 0 === 08 // also true! (Sonofabitch/Facepalm) * Infinity Of all the areas of a language in which one could gain expertise in, I thin…

I've been programming in PHP for a long time and I've never ever accidentally used an octal anywhere. Does such a non-problem really need this much attention?

Re: These are things in PHP which make me sad

#112
post #38
post #20

Earlier quoted context omitted.

Wow, a downvote within 30 seconds of posting? "php" == 0 returns true, and this makes sense how?

It makes sense for the same reason it makes sense in Perl: use a string as a number and you get the number that is at the start of the string. E.g., "12" == 12, and "12php" == 12.

But Perl has both "eq" and "==". PHP doesn't.

Re: These are things in PHP which make me sad

#113
post #40
post #32

Earlier quoted context omitted.

Because the string is first converted to an integer. "==" makes the comparison after converting types (see: Type Juggling in the docs) where "===" requires that the types be the same in order to be equal. Kind of weird the first time you see it, but it's a language design choice and does make sense once you understand it.

I think the reason people object to this behavior, even when they do understand it is that it makes the obvious default (==) dangerous. It's especially dangerous in the hands of the sort of non-experts for whom it's intended to make life easier.

== really isn't that dangerous unless you use it dangerously. Comparing non-numeric strings to numbers is already odd.

Re: These are things in PHP which make me sad

#114

PHP is Open Source. Compiling a list like this is nice, but contributing something back, and getting involved in making improvements to PHP, would be nicer.

The core team has it's own priorities, and has a history of ignoring the requests of the dev community. A couple of examples: in version 5.3, they added `goto`. WTF. Who, after five major releases, decides that what a language really needs is `goto`.

In the same release, they decided to finally add namespaces. But, despite the tired claim that PHP takes cues from C/C++/Java, they did not reuse :: (aka paamayim nekudotayim) as the scope resolution operator. Instead, they devised a new operator: '\'.

There is an IRC log somewhere where you can read the painful arguments back and forth over what this operator should have been. One of the rationalizations for the backslash was that it makes sense to Windows developers, as they are already familiar with the idea of '\' being used to separate elements of a hierarchy.

The end result:

Java:

Attribute/Method access: foo.bar

Static method access: Foo.bar

Package access: foo.bar.baz

C#:

Attribute/Method access: foo.bar

Static method access: Foo.bar

Namespace access: foo.bar.baz

Python:

Attribute/Method access: foo.bar

Static method access: Foo.bar

Module access: foo.bar.baz

PHP:

Attribute/Method access: $foo->bar

Static method access: Foo::bar

Namespace access: foo\bar\baz

Re: These are things in PHP which make me sad

#115
post #16

There's a lot of valid points there, but "#33 Cannot override private methods with a subclass" is the right behavior. That's exactly what private is for, and it's important to know that such names are non-colliding and you can rely on their implementation. Use protected for overridable methods. You shouldn't mock or directly test private methods in unit tests — they're not part of the interface!

public/protected/private is a dumb idea in dynamic languages. If you don't want someone calling your method, prefix it with an underscore and say "the results are undefined if you call methods that start with an underscore". Done. Easier to maintain, easier to test, less code to type in. Come to think of it... public/protected/private is a dumb idea in C++ and Java, too.

No kidding, I hate having to write some reflection BS in C# just to call a 'private method' as if addresses in memory cared about such things. The worst is some of the MS extensions for WIN32 that have private constructors designed to prevent obvious functionality. (Basically the constructors are private for political reasons). IIRC, it was something to do with reading COM streams from .msg files. It really ticked me off.

Re: These are things in PHP which make me sad

#116

Before everyone goes all "why don't you switch to language X or platform Y or lisp variant Z" I just want to say good job on this. These all seem to be sane critiques of PHP without being all doomsday world-ending inflamatory. Kudos. Also, I think that this is another example of how hard it is to build up a large and widely-used language/framework without having lots of warts. Especially since PHP wasn't originally d…

yeah.. php really has gotten a bad rap.. nearing ASP proportions

By ASP you mean VBScript, right? Because ASP pages can be written in JScript as well, and I remember JScript was quite nice.

Re: These are things in PHP which make me sad

#117

Earlier quoted context omitted.

> It doesn't make sense unless you understand how PHP works ... In other words: equality is not intuitive. Awesome! --- Let's take a look at "how PHP works": 0 == 08 // true! WTF, really? Well, let's just use the magical 'I-mean-actual-equality-not-some-other-kind-of-equality' operator: 0 === 08 // also true! (Sonofabitch/Facepalm) * Infinity Of all the areas of a language in which one could gain expertise in, I thin…

I've been programming in PHP for a long time and I've never ever accidentally used an octal anywhere. Does such a non-problem really need this much attention?

Perhaps you ask a user to enter the month of their CC expiration date, which is shown as 08/14 on their card.

  $v = validated_integer($user_input); // the user input 08
  if($v == 0 || $v === 0) { // let's just be safe
    
    // the Wrong Thing happens
    
  }
This is a contrived example, sure. But you've never checked to see if a user input the number 0? Or a non-zero, positive integer?

Re: These are things in PHP which make me sad

#118
post #34

Earlier quoted context omitted.

It doesn't make sense unless you understand how PHP works - at which point you'd use ===.

> It doesn't make sense unless you understand how PHP works ... In other words: equality is not intuitive. Awesome! --- Let's take a look at "how PHP works": 0 == 08 // true! WTF, really? Well, let's just use the magical 'I-mean-actual-equality-not-some-other-kind-of-equality' operator: 0 === 08 // also true! (Sonofabitch/Facepalm) * Infinity Of all the areas of a language in which one could gain expertise in, I thin…

That's the grammar for PHP integer literals[1]:

    decimal     : [1-9][0-9]*
                | 0
    
    hexadecimal : 0[xX][0-9a-fA-F]+
    
    octal       : 0[0-7]+
    
    integer     : [+-]?decimal
                | [+-]?hexadecimal
                | [+-]?octal
"08" and "09" do not match any of these.

    php -r "var_dump(08);"
Yields "int(0)", while the case should rather be treated as a syntax error, if you ask me.

The bottom line probably is that your literals just shouldn't have leading zeroes…

[1] http://www.php.net/manual/en/language.types.integer.php

Re: These are things in PHP which make me sad

#119

Earlier quoted context omitted.

I've been programming in PHP for a long time and I've never ever accidentally used an octal anywhere. Does such a non-problem really need this much attention?

Perhaps you ask a user to enter the month of their CC expiration date, which is shown as 08/14 on their card. $v = validated_integer($user_input); // the user input 08 if($v == 0 || $v === 0) { // let's just be safe // the Wrong Thing happens } This is a contrived example, sure. But you've never checked to see if a user input the number 0? Or a non-zero, positive integer?

I'm not sure what you expect to happen there but the following code always produces "Correct!":

    $user_input = '08';
    $user_input = (integer)$user_input;
    if ($user_input == 8) echo 'Correct!';
    if ($user_input == 0) echo 'Incorrect!';
If you take out the cast, the result is the same. If you change the numbers to '010' and 10 respectively, the result is also "Correct!". There is no weirdness.

Re: These are things in PHP which make me sad

#120
post #78

PHP is Open Source. Compiling a list like this is nice, but contributing something back, and getting involved in making improvements to PHP, would be nicer.

There's an outstanding request for better array syntax. It's been out there for over 3 years and received a lot of support from the community. At least one implementation has been submitted. But the core dev team rejected the idea, as they are convinced having second syntax available would harm ease of use (and thus popularity) of the language. In general, some aspects of PHP will not be fixed/improved for reasons of…

This is being revisited as per the PHP 5.4 TODO. My guess is it'll happen this time around, and I voted against it previously (but changed said vote). https://wiki.php.net/todo/php54
Post reply on HN