0

Not sure if this is possible. For example:

$color = isset($product['color']) ? $product['color'] : '';
$size  = isset($product['size']) ? $product['size'] : '';
$qty   = isset($product['qty']) ? $product['qty'] : '';
***
***

I use isset() pretty much everywhere I need to get a value from array that I am not sure if its set. I am using my own custom framwork so I was wondering if I can add some kind of way to check automatically if its set or not.

So I want something like this:

//if its not set, do not throw warning message just return blank
$color = $product['color'];
$size  = $product['size'];
$qty   = $product['qty'];

Basically similar to what PHP magic function __isset does for the class but I need it for any array that is not a class.

I added a function to my BaseController that is used in every controller but it is still an extra function call:

$color = $this->isSet($product, 'color', '');
$size  = $this->isSet($product, 'size', '');
$qty   = $this->isSet($product, 'qty', '');

and function:

public function isSet(array $data, $key, $return) {
   return isset($data[$key]) ? $data[$key] : $return;
}

My question is, is there a better way of doing this without functions?

GGio
  • 7,563
  • 11
  • 44
  • 81
  • 1
    You can turn off the warnings but it's really better to use `isset()`, `type hinting` and even `assert()` when you expect a variable. – Daniel W. Jun 09 '14 at 17:09
  • Why not set up a class with the var's initiated and fill them when needed? – Brett Santore Jun 09 '14 at 17:09
  • @BrettSantore for the example I brought yea I could set up a class, but there are many other places where I can not use classes its just an array and having a class for every array seems overhead to me – GGio Jun 09 '14 at 17:10
  • `__isset()` exists as a magic method in the same way that `__set()` and `__get()` exist; but it's for object properties, not arrays – Mark Baker Jun 09 '14 at 17:10
  • You can always temporarily suppress the warning messages, but *that's a terrible thing to do*. I think a sanitizing function is a safe and sane way to go about this problem. Besides, I imagine you'd want the default values to differ depending on the variable (e.g. `$qty` should default to `0`, but `$name` should default to blank) – Mr. Llama Jun 09 '14 at 17:11
  • Related: [Is there an idiomatic way to get a potentially undefined key from an array in PHP?](http://stackoverflow.com/q/23502348/1438393) – Amal Murali Jun 09 '14 at 17:12
  • The issue with the syntax that you propose is that it is not self-documenting that you are checking for an array property and setting to blank if it doesn't exist. You could spiral into checking `$color` exists or isset. It just strikes me oddly. – Jay Blanchard Jun 09 '14 at 17:23
  • Probably a very roundabout way, but you could also create a class that implements the [ArrayAccess](http://www.php.net//manual/en/class.arrayaccess.php) interface and return a default value you define in the implemented methods. I wouldn't say to do this for all arrays, but if you had a specific array like for products it would work. – Jonathan Kuhn Jun 09 '14 at 17:39

1 Answers1

-3
$color = @$product['color'];
$size  = @$product['size'];
$qty   = @$product['qty'];
Charles
  • 427
  • 3
  • 6
  • All you're doing is suppressing the warning instead of setting the variable to blank as the OP suggests. – Jay Blanchard Jun 09 '14 at 17:24
  • @JayBlanchard, well that's true, but I see no other way of accomplishing this without using a function, and really a blank variable is empty. – Charles Jun 09 '14 at 17:28
  • 5
    I'm sorry, but I __have__ to down vote an answer that uses error suppression. – vascowhite Jun 09 '14 at 17:52