Question

I'm creating a search-function for my PHP-based file manager. I'm getting this error: 'Catchable fatal error: Object of class Closure could not be converted to string' on the following line:

if ($data->input_ext)
{
    $data_ext = ($begun ? ($data->input_logic ? ' OR ' : ' AND ') :
    function ()
    {
        $begun = true;
        return "";
    }) . 'ext = "' . $data->input_ext . '"';
    $data_string.= $data_ext;
}

That's part of what builds the SQL query. $begun_files simply determines whether or not to put 'OR' or 'AND' at the beginning based on whether or not the user input a name or anything that comes before this to match. I have a feeling that I'm not allowed to include anonymous functions in ternary expressions but what should I do instead?

Thanks!

Was it helpful?

Solution

You can't use anonymous functions for inline flow control; just use a regular if statement and don't shun writing things on multiple lines:

if ($data->input_size) {
  if ($begun_files) {
      $str .= $data->input_logic ? ' OR ' : ' AND ';
      $begun_files = true;
  }
  $str .= sprintf('size %s "%f"',
      $data->input_size_op ? '<=' : '>=',
      $data->input_size * pow(1024,$data->input_size_unit)
  );
}

OTHER TIPS

Building off of the previous answer I ended up going with this:

if ($data->input_ext) { 
    if ($begun) { $logic = $data->input_logic ? ' OR ' : ' AND '; } else { $logic = ""; $begun = true; }        
    $data_ext = $logic.'ext = "'.$data->input_ext.'"'; $data_string .= $data_ext;
}

if ($data->input_size) { 
    if ($begun) { $logic = $data->input_logic ? ' OR ' : ' AND '; } else { $logic = ""; $begun = true; }
    $data_size = $logic.'size '.($data->input_size_op ? '<=' : '>=').' '.($data->input_size * pow(1024,$data->input_size_unit)); $data_string .= $data_size;
}

Thanks!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top