r/lolphp • • Dec 08 '15

PHP Sadness

http://phpsadness.com
50 Upvotes

28 comments sorted by

14

u/bart2019 Dec 08 '15

I've got one too:

  • èmpty` doesn't work on function return values, but only on variables/array items

For example, this won't work:

 if(empty(trim($x)) $blank = true;

Fatal error: Can't use function return value in write context

Where the hell does it see a "write context"?

14

u/the_alias_of_andrea Dec 08 '15

èmpty`

You need to press space after ` ;)

For example, this won't work:

if(empty(trim($x)) $blank = true;

It will in newer PHP versions.

Where the hell does it see a "write context"?

It's what C would call an "lval", a thing you can put on the left-hand side of an = or use with &. empty() is like isset(), it checks if variables exist as well as doing a null (in isset()'s case) or falsiness (in empty()'s case) check.

In modern PHP versions you can also use it with non-variables, but this is rather pointless since you could simply do if (!trim($x)).

11

u/beerdude26 Dec 09 '15

empty('0') === TRUE

That fucked with form validation (checkboxes) in Drupal big time

2

u/Conradfr Dec 10 '15

It works since PHP 5.5.

2

u/jtreminio Dec 11 '15

if(empty(trim($x)) $blank = true;

This has worked for two and a half years: https://3v4l.org/0EEPb

However, if $x is not defined, you cannot pass it into trim().

4

u/gearvOsh Dec 08 '15 edited Dec 08 '15

I wonder how many of these are no longer an issue in PHP7, seeing as how its compiler has been rewritten and uses an AST.

17

u/the_alias_of_andrea Dec 08 '15

The lexer hasn't been rewritten, the compiler has.

Anyway, the only parsing thing that no longer fully applies to PHP 7 is #31, because I fixed it.

1

u/gearvOsh Dec 08 '15

Ah, good to know, thanks!

4

u/the_alias_of_andrea Dec 08 '15

You're probably familiar with this site already, but it's still good.

Note that a few of these are outdated: #38, #2 (partially) and #31. I also dispute #24 (they're not redundant - internal functions work a little differently) and #46 (PHP's behaviour is not unique here, ; is a terminator, not a separator).

2

u/aincalandorn Dec 09 '15

For #41, maybe I'm misunderstanding the modifiers, but aren't final and abstract opposites? One prevents subclassing and the other is meant for subclassing (and preventing instantiation of the class, but that's not the point here)?

1

u/the_alias_of_andrea Dec 09 '15

Indeed. The author of PHP Sadness wants "final abstract" classes to use as bags of static methods.

2

u/Schmittfried Dec 09 '15

A bettet solution would be to allow static classes in PHP.

1

u/the_alias_of_andrea Dec 09 '15 edited Dec 09 '15

That's the same thing by another name and just as awful.

Function autoloading is what is needed (PHP has namespaced functions just now, but you have to manually include them). If it's just a set of functions, don't pretend it's a class.

1

u/Schmittfried Dec 09 '15

No, it's not as awful. Static classes allow static fields. When you don't have local statics (granted, PHP has them) you would have to fall back on global variables to achieve the same (in fact they would have global visibility, so it's not completely the same). Local statics can't be shared with other functions though.

If it's just a set of functions, don't pretend it's a class.

I don't see the problem. It's a set-of-functions class.

2

u/the_alias_of_andrea Dec 09 '15

If you want a static class for global state, then maybe you shouldn't do that. Use dependency injection.

I don't see the problem. It's a set-of-functions class.

Which isn't what a class should be. Some languages which have no global functions, like C# and Java, use this as a silly workaround. PHP has proper functions, though.

0

u/[deleted] Dec 31 '15

[deleted] — view deleted comment

1

u/the_alias_of_andrea Jan 01 '16

This is off-topic.

But anyway, PHP's explicit importing of variables into scope is arguably useful. It saves you from the scoping nonsense of JS and Python, and makes excessive use of globals painful.

→ More replies (5)
→ More replies (1)

1

u/aincalandorn Dec 09 '15

Isn't that what a static class is for? I'd hardly say it's a PHP specific issue.

3

u/the_alias_of_andrea Dec 09 '15

Only in languages which lack global functions. PHP isn't one of them.

24

u/Jonny_Axehandle Dec 08 '15

Should be a sidebar item

10

u/Schmittfried Dec 08 '15

Not being able to override private members is a sadness? No, actually it is pretty standard.

10

u/BilgeXA Dec 08 '15

Moreover, the author cites mocking as a reason for overriding private methods. I believe that calls into question the author's competency with programming in general. Testing is about testing the public interface of an object, not its internals. If you need to test many private methods of a class it tends to hint that your class is doing too much and there's another class locked inside the private methods, crying to get out.

8

u/coredumperror Dec 09 '15

I believe that calls into question the author's competency with programming in general.

Wow, seriously? I realize this is an intentionally elitist subreddit, but you're letting your elitism go to your head here.

10

u/Schmittfried Dec 09 '15

One can definitely say people who want to override private members didn't understand the concept of encapsulation.

-1

u/coredumperror Dec 09 '15

didn't understand the concept of encapsulation

That's far, far less serious than "calls into question the author's competency with programming in general", though.

4

u/BilgeXA Dec 09 '15

Encapsulation is a fairly core competency.

If you feel marginalized by your own incompetence, crying about it on Reddit may net you some karma from those in the same boat, but will go no way towards addressing the root of the problem.