Is there a way to avoid overwriting long operands in if statements?

I constantly find myself if such statements look like this:

if(isset($this->request->data['Parent']['child'])){ $new_var['child'] = $this->request->data['Parent']['child']; } 

My question is that without creating a new shorter variable before the if there a magical php process that can help avoid overwriting $this->request->data['Parent']['child'] in the body of the if statement? I also want the code to be clean for any successor developers.

I am also not looking for a triple solution. Something else a bit along the line of using jquery this . (What I know) php does not allow anonymous functions, or it is not a common practice, and I am not saying that I need a bunch of anonymous functions for all my classes.

+4
source share
5 answers

It would be extremely cool, but, unfortunately, nothing can do it.
Perhaps some plugin for some IDEs can help you with this.

What I can offer in this case is what I do personally, without touching the mouse, moving the arrow cursor next to $this , then hold CTRL + SHIFT and the right arrow and you will choose most at a time . (Then just use ctrl + c and ctrl + v to copy the paste)

Of course this applies to all languages, not just PHP

+2
source
 var $a = "" ; function __construct(){ if(isset($this->request->data['Parent']['child'])){ $this->a = "1"; } } 

now use

 if(!empty($this->a)){ // ur code } 
0
source

You can create a variable in the same condition block and use it after words

 if($child = $this->request->data['Parent']['child']){ $new_var['child'] = $child; } 

The only difference here is that if $this->request->data['Parent']['child'] set but FALSE (or an empty string, NULL or 0), the test will fail

0
source

Although there are several ways to make your code shorter, I would advise against such methods. The reason is that most of these methods make the code much less comprehensible to the next person.

I would recommend using a good IDE with code completion, it would help to type long names, saving the code for the next person to look at it.

0
source

I'm not quite sure what you are really looking for, but you can use a custom object to store your data that does not generate notifications when trying to access non-existent keys.

 class Data { function __get($key) { return null; } } 

This works because __get is only called when the key does not exist.

0
source

All Articles