0

So I I've tried to make this code log an exception but it gives me the error message object of class domain_model could not be converted to string

The function looks as follows:

function errorLog($log, $error_type, $string, $file, $row, $error_hash, $error_trace)
   {
   $text = $error_hash . ' (' . date('Y-m-d H:i') . "):\n[Error type]: " . $error_type .   "\n[Message]: " . $string . "\n[File]: " . $file . "\n[Row]: " . $row . "\n";
   $text .= "[Trace]:\n";
   foreach ($error_trace as $t)
      {
      $text .= ((isset($t['type']) && isset($t['object'])) ? $t['object'] . $t['type'] . $t['function'] : $t['function']) . "\n";
      $text .= $t['file'] . "\n";
      $text .= $t['line'] . "\n";
      $text .= print_r($t['file'], 1) . "\n";
      }
file_put_contents(BASE . $log . '.log', $text, FILE_APPEND);
}

After alot of thinking eventually saw that the line in making a mess is infact this one:

$text .= ((isset($t['type']) && isset($t['object'])) ? $t['object'] . $t['type'] . $t['function'] : $t['function']) . "\n";

And as I see it the only one in need of conversion should be $t['object'] however using (string)$t['object'] didn't work and still gives me the same error. Is there any other solution on how to convert it to a string than this?

I've looked at how they suggest it to be done here

Community
  • 1
  • 1
  • 1
    Which var has an object of class domain_model? – Reflic Dec 19 '13 at 10:23
  • From what I would understand it should be the $exception_obj->getMessage() since that is even called string in the function. Unfortunatley it isn't me who wrote the code from the begining and I have no way to contact the person who did so I only wrote what I am certain of. I might add that this should log an error or in this case exception to me but because of this problem it won't. – user2991184 Dec 19 '13 at 10:27
  • to avoid this, you need either add __toString() method to $t['object']; or use print_r($t['object'], true) or maybe serialize( $t['object']) to convert object to string – pomaxa Dec 19 '13 at 14:24
  • Thank you I will test it as soon as possible and if I'm not completly wrong this won't be needed with the isset statment since that checks if it's null or not. – user2991184 Dec 19 '13 at 14:39

0 Answers0