Skip to content

Commit

Permalink
Avoid "Redefining already defined constructor" in DB_Error.
Browse files Browse the repository at this point in the history
  • Loading branch information
convissor committed Nov 23, 2015
1 parent 575bdf1 commit 7df34b9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
25 changes: 14 additions & 11 deletions DB.php
Original file line number Diff line number Diff line change
Expand Up @@ -948,17 +948,6 @@ class DB_Error extends PEAR_Error
{
// {{{ constructor

/**
* Class named constructor in case someone is calling it directly
*
* @return void
*/
function DB_Error($code = DB_ERROR, $mode = PEAR_ERROR_RETURN,
$level = E_USER_NOTICE, $debuginfo = null)
{
$this->__construct($code, $mode, $level, $debuginfo);
}

/**
* DB_Error constructor
*
Expand All @@ -982,6 +971,20 @@ function __construct($code = DB_ERROR, $mode = PEAR_ERROR_RETURN,
}
}

/**
* Workaround to both avoid the "Redefining already defined constructor"
* PHP error and provide backward compatibility in case someone is calling
* DB_Error() dynamically
*/
public function __call($method, $arguments)
{
if ($method == 'DB_Error') {
return call_user_func_array(array($this, '__construct'), $arguments);
}
trigger_error(
'Call to undefined method DB_Error::' . $method . '()', E_USER_ERROR
);
}
// }}}
}

Expand Down
7 changes: 6 additions & 1 deletion tests/db_error.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ $e = new DB_Error(DB_ERROR); print strtolower($e->toString())."\n";
$e = new DB_Error(DB_ERROR_SYNTAX); print strtolower($e->toString())."\n";
$e = new DB_Error(DB_ERROR_DIVZERO); print strtolower($e->toString())."\n";

print "testing instantiated method...\n";
$e = new DB_Error;
$e->DB_Error("test instantiated"); print strtolower($e->toString())."\n";

print "testing different error modes...\n";
$e = new DB_Error(DB_ERROR, PEAR_ERROR_PRINT); print strtolower($e->toString())."\n";
$e = new DB_Error(DB_ERROR_SYNTAX, PEAR_ERROR_TRIGGER);
Expand All @@ -53,7 +57,6 @@ print "testing different error serverities...\n";
$e = new DB_Error(DB_ERROR_SYNTAX, PEAR_ERROR_TRIGGER, E_USER_NOTICE);
$e = new DB_Error(DB_ERROR_SYNTAX, PEAR_ERROR_TRIGGER, E_USER_WARNING);
$e = new DB_Error(DB_ERROR_SYNTAX, PEAR_ERROR_TRIGGER, E_USER_ERROR);

?>
--GET--
--POST--
Expand All @@ -65,6 +68,8 @@ testing different error codes...
[db_error: message="db error: unknown error" code=-1 mode=return level=notice prefix="" info=""]
[db_error: message="db error: syntax error" code=-2 mode=return level=notice prefix="" info=""]
[db_error: message="db error: division by zero" code=-13 mode=return level=notice prefix="" info=""]
testing instantiated method...
[db_error: message="db error: test instantiated" code=-1 mode=return level=notice prefix="" info=""]
testing different error modes...
DB Error: unknown error[db_error: message="db error: unknown error" code=-1 mode=print level=notice prefix="" info=""]

Expand Down

0 comments on commit 7df34b9

Please sign in to comment.