7

When "future-proofing" code by testing it on PHP 5.4, I get a warning I don't 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 broke out $condition['field'] into its own row to reduce the amount of code to focus on. About that specific line, PHP has this to say

Warning: Illegal string offset 'field' in DatabaseCondition->__clone()

And I just can't see how 'field', is an illegal string offset. I'm guessing that I'm just missing something obvious, but if the community can't find a problem, I'll file a bug report.

I interpret the warning as "Under no circumstances is 'field' a valid key". This error would have made sense if I had tried to us for example an array as a key.

Letharion
  • 4,067
  • 7
  • 31
  • 42
  • Code Review is for improving working code. Specific questions about the meaning of warnings are off-topic there. – Winston Ewert Feb 17 '12 at 21:13
  • 1
    Can you `var_dump('$this->conditions');`? – Mike Purcell Feb 17 '12 at 21:29
  • This error also occurs when installing Drupal under PHP 5.4 http://drupal.org/node/1483986 I believe something in PHP 5.4 is broken, or perhaps 'field' is now a reserved PHP keyword that cannot be used for arrays, but I'm leaning towards a PHP bug. It is reported that this error does not occur in previous versions on PHP. – Brain2000 Mar 21 '12 at 15:14
  • If you read my comment under the accepted answer, you will find that this is not a PHP problem. The problem has been solved as of Drupal 7.13 http://drupal.org/node/1414412 – Letharion Mar 21 '12 at 15:53

2 Answers2

3

Without more knowledge about the creation of the conditions array/iterator, I can only assume that you should first check if the offset exists.

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

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

  • This is probably me being slow, but I don't see how that applies? If this had been a notice about 'field' not existing, I would have had no problems backtracing the issue and finding the source of the problem. But now, the way I read the error, _under no circumstances is 'field' a valid key_; that doesn't make sense to me. Added this to the question as well. – Letharion Feb 17 '12 at 19:14
  • if $condition is no array or array object implementation, then you are trying to access string and you can't access the string char array with an string as key and get the illegal string offset error, it's new to me to see that error on 5.4 mostly i see it on the 6.0 branch... – Tobias Herkula Feb 18 '12 at 22:49
  • The value of $condition can be 'AND' for instance. – olleolleolle Nov 29 '12 at 06:56
2

The warning looks like its saying that $condition is a string. Without any knowledge of the code I don't whether that makes any sense.

Winston Ewert
  • 44,070
  • 10
  • 68
  • 83
  • Bah, you're right. As suggested by "Mike Purcell" above, I var_dump $conditions when the error happens, and it seems it sometimes becomes a string. – Letharion Feb 19 '12 at 21:46