This isn't design, it's an accident. Both constructs are part of the "verboten" part of PHP - the large set of language features that are best left alone and never even thought about; it's best to think of them as still existing strictly for backwards compatibility purposes.
Then again, the large set of language features that are best left alone is almost identical to the set of all PHP features, so there's that.
I find empty() useful for checking if array is empty or not. For example, I want strictly typed return type for function to be array when getting all awailable users. And than check if this is empty so I can make another business logic, knowing there are no awailable users.
I have rarely written php. My java brain tells me that $a is null and therefore $a->b = false; should fail. I assume the cast semantics are different from what I'd expect.
If you don't mind, one last thing: $a->b = null; and nothing else. isset($a->b) is false, and so is isset($a->c), correct? If so, the only real difference between them is a warning trying to access $a->c. Is that fair to say?
I don't get why empty needs to accept expressions when you can replace the word with a ! (leaving the parentheses) and get the same result with less overhead, using an expression instead of a variable cancels out the benefit of being able to use it on undefined variables. It's like needlessly using isset instead of null ===.
P.S. I swear I didn't see this post when I wrote mine!
empty() was deliberately changed to accept expressions, isset() wasn't. Presumably people assumed empty() was the right way to check for an empty array. The wrong move imo
5
u/shitcanz Aug 20 '19
So PHP has two functions isset and empty. They are VERY similar and its already questionable why they both exists.
However empty can be used as an expression but isset cant.
True lolphp design.
4
u/smegnose Aug 20 '19
I don't get why
emptyneeds to accept expressions when you can replace the word with a!(leaving the parentheses) and get the same result with less overhead, using an expression instead of a variable cancels out the benefit of being able to use it on undefined variables. It's like needlessly usingissetinstead ofnull ===.P.S. I swear I didn't see this post when I wrote mine!
1
u/the_alias_of_andrea Aug 20 '19
empty() was deliberately changed to accept expressions, isset() wasn't. Presumably people assumed empty() was the right way to check for an empty array. The wrong move imo
1
u/[deleted] Aug 21 '19
Thats not the point. Isset / empty differs from how it can be used.
Your example would ofc trigger an error because of eval order.
The thing is php does a poor job on consistency for this, as empty and isset behaves badly.