How does my array index "Invalid line offset"?

When the code for "future verification", testing it in PHP 5.4, I get a warning that I do not understand.

function __clone() { $this->changed = TRUE; foreach ($this->conditions as $key => $condition) { if ( $condition['field'] instanceOf QueryConditionInterface) { $this->conditions[$key]['field'] = clone($condition['field']); } } } 

I burst $condition['field'] into my own line to reduce the amount of code I need to focus on. PHP has this to say about this particular line

Warning: Invalid 'field' line offset in DatabaseCondition->__clone()

And I just don’t see how the “field” is an illegal line offset. I assume that I will simply miss something obvious, but if the community cannot find the problem, I will write an error report.

I interpret the warning as “under no circumstances is the field a“ valid key. ”This error would make sense if I tried, for example, an array as a key.

+7
source share
2 answers

The warning is similar to its statement that $condition is a string. Without any knowledge of the code, I don't understand if it makes sense.

+2
source

Unaware of the creation of an array / iterator of conditions, I can only assume that you should first check if an offset exists.

 if(isset($condition['field']) && $condition['field'] instanceOf QueryConditionInterface) 

Using isset in this situation is faster and faster than array_key_exists, the only difference is that if the condition $ [field] is NULL, iset will return fall, array_key_exists will return true because the key exists. But since you only want to work with fields that are an instance of QueryConditionInterface, you will do fine with isset.

+3
source

All Articles