Re: PEAR::setErrorHandling in classes
| From: | Stig S. Bakken | Date: | Wed, 27 Feb 2002 08:54:37 +0000 |
| Subject: | Re: PEAR::setErrorHandling in classes | ||
| References: | 1 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-4887@lists.php.net to get a copy of this message | ||
On Wed, 2002-02-27 at 00:18, Rich Payne wrote:
>
> I sent this to pear-general a few days and didn't get a response...hoping
> someone here can help me.
>
> I'm in the process of converting some code I wrote to use PEAR DB instead
> of direct mysql access and have come accross something I don't understand.
> I've been reading through Tomas V.V.Cox's intro to PEAR DB (it's been very
> helpful BTW) and in there he has:
>
> // You can also use an object as pear error handler so:
> // setErrorHandling(PEAR_ERROR_CALLBACK, array($object,'method_name');
>
> OK, so I used that and it never calls the function, if I misspell the
> function name then PHP complains about the function not existing, but if
> they match it never seem to get called. So, some example code:
>
> Simple functional example, works great:
> <?php
> include_once 'DB.php';
> $db_conn_data = array(
> 'phptype' => "mysql",
> 'hostspec' => "localhost",
> 'database' => "test",
> 'username' => "root",
> 'password' => " "
> );
>
> PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'handle_db_error');
> $link = DB::connect($db_conn_data);
>
> function handle_db_error (&$obj) {
> echo "Test:: ".$obj->getMessage()."\n";
> }
> ?>
>
> Class example, fails:
>
> <?php
>
> include_once 'DB.php';
>
> class test {
> function test() {
> $db_conn_data = array(
> 'phptype' => "mysql",
> 'hostspec' => "localhost",
> 'database' => "test",
> 'username' => "root",
> 'password' => " "
> );
>
> PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($this,'handle_db_error'));
> $link = DB::connect($db_conn_data);
> //var_dump($link);
> }
>
> function handle_db_error (&$obj) {
> echo "Test:: ".$obj->getMessage();
> }
> }
> ?>
>
> I'm just using another script to create an object of type test. Again, the
> handle_db_error function never actually seems to get called.
>
> Any help would be much appreciated.
You just found one of the inconsistencies in PHP 4's object model.
setErrorHandling() call be called in two ways, as
PEAR::setErrorHandling(), or as $this->setErrorHandling() in objects
that inherit the PEAR class. The way setErrorHandling determines how it
is called is to see if $this is set. Unfortunately, PHP defines the
calling object's $this even in static methods in other classes, so in
your example this simple doesn't work. PEAR::setErrorHandling() will
only work reliably when called from outside a class (global scope or in
a regular function). In objects, you should inherit PEAR in the object
you want to catch errors from and user $object->setErrorHandling()
instead.
With a fairly recent PHP, you can do this instead though:
<?php
class test { ... }
PEAR::setErrorHandling(PEAR_ERROR_CALLBACK,
array("test", "handle_db_error"));
$obj = new test;
?>
- Stig