4

I find myself constantly writing if statements like the following:

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

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

I am also not looking for a ternary solution. Something a little more along the lines of jquery's use of this. (That I know of) php doesn't allow anonymous functions or it's not common practice and I'm not saying I want a bunch of anonymous functions all over my classes either.

UPDATE Added in php 7 What does double question mark (??) operator mean in PHP

Tim Joyce
  • 4,487
  • 5
  • 34
  • 50
  • Anonymous functions are allowed (at least in callback context, you may need create_function() in older versions) and there are lambda functions as of... 5.4? As for your question I do not think this is possible. If you are in class context you could use the magic __get and __set functions to implement a shorter version of variables maybe. – Hikaru-Shindo Dec 17 '12 at 10:45
  • what about error suppression ? – CSᵠ Dec 17 '12 at 10:46
  • Error supression is a really bad practice. I would not recommend it. – Hikaru-Shindo Dec 17 '12 at 10:47
  • 1
    You could use `try/catch` (in certain cases). Won't be much less code, but the accessor would only occure once. – Yoshi Dec 17 '12 at 10:48
  • 1
    As an aside, I don't know if your intention is to use 'isset' to check presence of array key in data['Parent'] but if so, I'd avoid it as it will evaluate to false if data['Parent']['child'] is null e.g. https://gist.github.com/4317463. You will be better off using array_key_exists. – Kevin Dec 17 '12 at 11:06
  • While you said that you don't want to create a shorthand variable before the `IF`, is that really such a bad thing? Using a reference and doing `$child = &$this->request->...`, then `if(isset($child)) {}` seems to work fine and doesn't throw errors if one of the array/object entries is not set. – RickN Dec 17 '12 at 11:16

5 Answers5

2

This would be extremly cool, but unfortunaly nothing can do it.
Maybe some plugin for some IDE can help you doing stuff like that.

What I can suggest on this case is what I do personally, without touching mouse move cursor with the arrows near $this then hold CTRL + SHIFT and right arrow and you will be selecting more stuff at once. (Then just use ctrl+c and ctrl+v to copy paste)

Of course this applies to every languages, not only PHP

dynamic
  • 46,985
  • 55
  • 154
  • 231
0
var $a = "" ; 
 function __construct(){ 
   if(isset($this->request->data['Parent']['child'])){
         $this->a = "1";
    }
   }

now use

if(!empty($this->a)){
// ur code
}
crynaldo madrid
  • 638
  • 2
  • 7
  • 16
  • Don't use `empty` when you know the variable exists. `if ($this->a)` will do just fine in your case. – deceze Dec 17 '12 at 10:46
0

You can create a variable in the same condition block, and use it afterwords

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

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

BnW
  • 592
  • 1
  • 3
  • 13
  • This tends to be the reason why assignment inside of a conditional best avoided. – Kevin Dec 17 '12 at 11:48
  • It is generally a bad idea to introduce side-effects in evaluations like these. Though this may be shorter, it will cost you in the long run. – Tarilo Dec 17 '12 at 12:02
0

Though there are a couple of ways to make your code shorter, I would advice against such techniques. The reason is that most of these techniques make the code a lot less clear for the next person to understand.

I would recommend using a good IDE with code completion, this would help with typing long names while keeping the code clear for the next person to look at it.

Tarilo
  • 430
  • 2
  • 6
0

I am not perfectly sure what you are really looking for, but you could use a custom object for storing your data in that does not generate notices when you try to access non-existent keys.

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

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

dualed
  • 10,262
  • 1
  • 26
  • 29