Is there a switch to disable "cannot use a temporary expression in the context of a record"?

The error was added in PHP7, and I have a problem with code such as:

(some complex expression)->my_property = 1 

Note that I assign the field of the object, not the object itself (assigning to a temporary object does not make sense, I agree, but here it is not ).

This gives me the error "cannot use a temporary expression in the context of the record." When I rewrite this as:

 $tmp = (some complex expression); $tmp->my_property = 1; 

Everything is fine. The problem is that I have to have one expression (assignment is an expression), and since PHP does not support the comma operator, now I'm fried with two operators .

This is a huge difference for me, because I cannot pass all the code as an expression further. Everything is valid inside this "complex expression", so I would just like to disable this check in PHP.

Is it possible? How?

My code is written automatically (it is generated), and the result of the expression is a valid PHP object, the problem in PHP for some reason does not notice it. In addition, the second form works.

Update: This is an example of the complex expression mentioned above:

 (($a = foo()) === 0 ? $a : bar()) 
+6
source share
3 answers

I'm going to say no.

Everything is valid inside this "complex expression", so I would just like to disable this check in PHP.

I think the problem is that refusing to write temporary expressions is not just a β€œcheck” in PHP, but part of the language since 7.1 (introduced in 5c2120b ).

Starting with 5c2120b , the language grammar has been updated to redefine what is heartless, like:

 dereferencable: variable { $$ = $1; } | '(' expr ')' { $$ = $2; zend_do_begin_variable_parse(TSRMLS_C); $$.EA = 0; } | dereferencable_scalar { $$ = $1; zend_do_begin_variable_parse(TSRMLS_C); $$.EA = 0; } ; 

- Source on GitHub

In addition, you will notice that the sources of these errors in the code base do not check anything before creating an error, for example. 1 and 2 .

A workaround would be to switch to a version of PHP that would allow this.

+3
source

I got this error while refactoring badly written code:

 class MyClass { static $cache = []; public function myFunction() { $this->cache['foo'] = 'bar'; } 

I rewrote this to fix the non-static link to $cache inside myFunction :

 class MyClass { static $cache = []; public function myFunction() { self::cache['foo'] = 'bar'; } 

Notice I skipped $ and had to write self::$cache['foo'] .

This caused the error cannot use temporary expression in write context , which was not particularly useful. In my case, there was nothing wrong with what I was trying to do, just a typo.

+6
source

It is very easy to understand. When you try to call Someclass :: somevar, it means that you are trying to call "const" var from the class, i.e. a constant variable, for example "define ()", but inside the class. But when yor'r calls Someclass :: $ somevar, you call a "static" var, similar to the $ someclass-> somevar object. This is just a very simple explanation for beginners or php dummies. For a better understanding, there are several examples http://php.net/manual/en/language.oop5.static.php

+1
source

All Articles