You are here

function ctools_var_export in Chaos Tool Suite (ctools) 6

Same name and namespace in other branches
  1. 7 includes/export.inc \ctools_var_export()

Export a field.

This is a replacement for var_export(), allowing us to more nicely format exports. It will recurse down into arrays and will try to properly export bools when it can, though PHP has a hard time with this since they often end up as strings or ints.

1 call to ctools_var_export()
ctools_export_object in includes/export.inc
Export an object into code.

File

includes/export.inc, line 655
Contains code to make it easier to have exportable objects.

Code

function ctools_var_export($var, $prefix = '') {
  if (is_array($var)) {
    if (empty($var)) {
      $output = 'array()';
    }
    else {
      $output = "array(\n";
      foreach ($var as $key => $value) {
        $output .= $prefix . "  " . ctools_var_export($key) . " => " . ctools_var_export($value, $prefix . '  ') . ",\n";
      }
      $output .= $prefix . ')';
    }
  }
  else {
    if (is_object($var) && get_class($var) === 'stdClass') {

      // var_export() will export stdClass objects using an undefined
      // magic method __set_state() leaving the export broken. This
      // workaround avoids this by casting the object as an array for
      // export and casting it back to an object when evaluated.
      $output = '(object) ' . ctools_var_export((array) $var, $prefix);
    }
    else {
      if (is_bool($var)) {
        $output = $var ? 'TRUE' : 'FALSE';
      }
      else {
        $output = var_export($var, TRUE);
      }
    }
  }
  return $output;
}