I am instantiating a class in PHP 7 only to immediately use a member and then discard the object. This is a class which accepts various inputs and has a toString method which generates a standard output string based upon its inputs. Some of the inputs are specific to the application and therefore are not supported by any common library. So I tried:
$output = new MyObj($input)->toString();
but this was rejected by PHP. Instead I am forced to do:
$mydate = (new MyObj($input))->toString;
but this is ugly and seems to contradict the documentation http://php.net/manual/en/language.operators.precedence.php which specifies that the new operator has the highest precedence of all operators, until one notices that the object operator (->) does not appear anywhere in the documentation of operators. In particular unlike every other operator there is no manual page for the object (->) operator. There does not even seem to be a consensus on what to call ->. If you google "PHP object operator" you get various helpful pages provided by volunteers, but not a single hit anywhere in the official documentation of PHP. If you are in the official documentation and type "object operator" into the search field you get no hits. The object operator is used in many examples throughout the PHP documentation, but it is never defined. Some people writing on this issue have even declared that contrary to the assumption made by almost all helpful documentation the object operator is not an operator! As a result I cannot declare that this behavior is a bug in PHP because PHP does not document what the correct behavior of -> is! Other posters have commented on this as well, all the way back to PHP 5.6. For example "Operator" precedence? Why new Object()->method() gives a syntax error in PHP? . The response on that query was "Because it's ambiguous, i.e. it can be interpreted in 2 different ways:" but it is only ambiguous because PHP does not DOCUMENT what the correct behavior of -> is! Where is the operator precedence, or any other aspect, of -> formally documented?