How to throw an array exception in php
To convert a complex data-structure like an array into a string (e.g. for error messages), you can make use of print_r
Docs and setting it's second parameter to TRUE
:
... ": " . print_r($_r['status'], TRUE) . "\n" ...
The problem is that You're trying to merge array with a string. It'll always end like this.
Maybe You should pass to exception an array, so You can later make use of it?
<?php
class myException extends Exception {
private $params;
public function setParams(array $params) {
$this->params = $params;
}
public function getParams() {
return $this->params;
}
}
// later it can be used like this:
try {
$exception = new myException('Error!');
$exception->setParams(array('status' => 1, 'errors' => array());
throw $exception;
}
catch (myException $e) {
// ...
}
?>
we can using json format
throw new Exception(json_encode(['type'=>'error','isExit'=>'true','title'=>'SystemConfigError']));
and in the catch
catch (Exception $error)
{
var_dump(json_decode($error->getMessage(),JSON_OBJECT_AS_ARRAY));
}