|| is a short-cutting or comparison operator which returns the first true thing it encounters.
| is a bitwise or combinator, which takes two sets of flags, and combines them into one set of flags that has all flags activated which were also active in the input sets.
Semantically, | is absolutely the correct choice here.
A better question would be why there's a $2 there, though i hope that's just a typo and means $e.
I wish. The fact that it actually doesn't is one of the things that really bugs me about PHP:
<?php
var_dump(0 || 42);
The above example will output: bool(true) $ php -r 'var_dump(0 || 42);'
bool(true)
$ php -r 'var_dump(0 ?: 42);'
int(42)edit: Just to clarify for anyone else unfamiliar with it:
$ php -r 'var_dump(42 ?: 0);'
int(42) $ php -a
Interactive shell
php > echo 1 || die('die');
1
This echo's 1, it doesn't die.I understand the use of pipes for bitwise, but in this case if you read the code in the example, we're basically saying "if the exception is of type A or of type B", personally I'd be inclined to use || if I didn't already know the syntax.
Choosing a pipe because it's easier for an obscure language feature, is not least astonishment. Commas are used for lists. The pipe is a bad choice among a nice feature.
... "catch (FooException | BarException" is new syntax that would be a include-time fatal in existing code
... "public const FOO" is new syntax that would be a include-time fatal in existing code; public is the default modifier in the language when no visibility modifier is present, so existing code functions exactly the same as before
... ": void" return type only conflicts with existing code if a class is named Void, which becomes an error for the file declaring the class ... this is vanishingly unlikely [0] and an easy grep to fix; if you're looking at some code "$var = i_return_void();", the annotation is a clear sign that the "$var =" part is obviously confused.
... " 5 + 'a string' " is nonsense code which codebases that care about warnings currently want to be aware of.
[0] even the few hits for https://github.com/search?utf8=%E2%9C%93&q=%22class+Void+%22... are mostly false positives.
Moreover, having these declarations actually be part of the language, even if the interpreter itself cannot enforce them ahead-of-time, means tooling can be built around it, and we can use them for optimisations in the VM (which PHP 7.1 does: we elide type checks on certain arithmetic operations for operands of known types).
If you want to ensure type safety before the program actually runs, you can of course let your IDE or static analyser check that for you.
I think types specifications in interpreted (not compiled) languages should be only hints (for IDEs, code analyzers and JIT compilation etc.). This type checking for every method probably also adds some overhead does it not?
EDIT: correction
Unenforced type hints don't give these guarantees. They have no requirement to be accurate because the runtime won't enforce them, so they're essentially documentation, not code. They can't be safely relied upon by the optimiser because they're unenforced. The code inside the function body can break at runtime if given the wrong type, so if having a specific type is particularly important, you have to add manual type checks or coercions yourself.
Furthermore, doing ahead-of-time checking would require infrastructure we don't currently have for providing information to the PHP interpreter about whether or not PHP files have passed type checks. But again, most code is not fully typed, and so this doesn't provide all that many guarantees to the interpreter.
Having methods that just have the right names is a pretty weak assurance, isn't it?
Visibility and typing features are good. Visibility helps to design and write better code. The typing features are also welcomed in my view. I would rather a request completely fails because a variable is the wrong type rather than PHP's type coercion leading to an unexpected result with no errors or warnings.
That said, I use PHP for my day job. I see no reason to start new projects in it. I never use it in my side projects. Introduction of the single pipe operator to represent OR instead of a double pipe operator is baffling in my opinion.
7.1 does more work to improve consistency and error handling which is definitely welcomed. The problem is there is much legacy crap in PHP. It only exists for web applications and there are a growing number of languages which do this better.
I kind of understand it since it's not a traditional OR operation and intends to keep and associate the matching values, but maybe something like
catch (Exception $e in (Exceptions...))
would make it clearer. Either way, it's so close to feeling like an OR operation that I feel like it will be a lot of debugging when you forget this.
The good it does is it stops your application from possibly continuing on with buggy code.
This is especially important in something like billing code. Imagine if your code was expecting an integer (bill amount) but got a string instead that via the types system evaluated to 0 or 1 instead of the real integer... In that case you might charge customers less or more than you should have and its a big deal trying to get all that fixed.
Better to just throw an error which you can see in an exception handling system like Sentry and take care of the problem.
Because they are easy to implement (function type hints, typed properties) and the php community have a collective, insatiable, perpetual hardon for features that are supposed to enhance correctness..
So the core devs are eager to propose these kinds of features. And the community is eager to accepts them without much thought..
To see how child like this mentality towards these type of features, see this comment, where a Php expert makes a big deal regarding when a user omitted mentioning the type of a value....
https://www.reddit.com/r/PHP/comments/4k2htl/ive_just_given_...
... PHP 7.1 introduces visibility modifiers to constants ...
... With PHP 7.1 it is now possible to specify that a function has a void return type, i.e. it performs an action but does not return anything ...
... Example four contains numeric values, so everything else is stripped out, and the sum of those are used to calculate the total of 10. But it would still be nice to see a warning, as this may not be intended behaviour ...
Good to see PHP continues to introduce new inconsistencies while striving to break existing code by fixing old ones. Best of both worlds!
edit: OK this was uncharitable and technically some of them are not inconsistencies, just things I thought were weird. I know PHP is widely used and considered useful and will stay with us forever. But I found those snippets interesting.