ID:               31798
 Updated by:       [EMAIL PROTECTED]
 Reported By:      vl409 at yandex dot ru
-Status:           Open
+Status:           Bogus
 Bug Type:         Documentation problem
 Operating System: linux-2.4.25,freebsd 4.10
 PHP Version:      4.3.10
 New Comment:

It's expected and documented behavior, take a look at the chapter
References Explained.

What's happening? &$this is a reference to the current object, but
"$x=new" assigns a copy of this object to $x. Thus error handler is now
method in object which is not referenced by any variable.

You can achieve the desired behavior by using &$this together with
$x=&new (or use PHP 5, but take a look at http://php.net/migration5.oop
first).


Previous Comments:
------------------------------------------------------------------------

[2005-02-02 16:31:12] vl409 at yandex dot ru

I thought about this problem and think that documentation should better
explain how objects in PHP are created.

I removed set_error_handler() call from constructor to method of error
handling class and called this method right after object creation. This
works.

So, what we should understand, is when object is actually created ? 

It seems to me that when in constructor i assign values to $this->smth,
$this is not the same to what returned when i call $obj=new Something,
that`s why set_error_handler(&$obj,"method") doesn`t work in this
case.

am i right? =)

------------------------------------------------------------------------

[2005-02-01 22:13:42] [EMAIL PROTECTED]

reclassified


------------------------------------------------------------------------

[2005-02-01 21:55:05] vl409 at yandex dot ru

Description:
------------
Possibly,it`s a duplicate bug of
 Bug #25128     set_error_handler not sending reference
http://bugs.php.net/bug.php?id=25128&edit=2

I tried to write my own class for handling errors in php and
found that if i call set_error_handler with argument, being
object(&$this), it is passed by value, not by reference.

This is strange, because function set_error_handler is
declared same as call_user_func, but behaves differently.

as defined in manual:
mixed call_user_func ( callback function...)
mixed set_error_handler ( callback error_handler...)

I tested example on FreeBSD 4.1/PHP 4.3.10 and Linux 2.4.25/PHP 4.3.5

Code,demonstrating this, provided below.

P.S. If it`s a duplication, then you can mean this report as BUG IN
DOCUMENTATION! I found that i wanted to do almost the same as man in
Bug #25128, but due to lack of documentation, spent a lot of time for
nothing...


Reproduce code:
---------------
<?php
class error_handler
{       var $error_count;
        function error_handler(){
                $this->error_count=0;
                set_error_handler(array(&$this,'handler'));
                echo "<b>New error handler registered!</b><br>";
        }
        function handler()
        {
                echo "Cought an error! increasing counter!<br>";
                $this->error_count++;}
        function show(){
                echo "This object handled ".$this->error_count." errors<br>";
        }
}

$x=new error_handler;

$x->show();

trigger_error("Ooops!",E_USER_ERROR);
$x->show();
trigger_error("Ooops!",E_USER_ERROR);
$x->show();
echo "<b>called by call_user_func:</b><br>";
call_user_func(array(&$x, "handler"));
$x->show();
echo "<b>called manually</b><br>";
$x->handler();
$x->show();

?>

Expected result:
----------------
New error handler registered!
This object handled 0 errors
Cought an error! increasing counter!
This object handled 1 errors
Cought an error! increasing counter!
This object handled 2 errors
called by call_user_func:
Cought an error! increasing counter!
This object handled 3 errors
called manually
Cought an error! increasing counter!
This object handled 4 errors

Actual result:
--------------
New error handler registered!
This object handled 0 errors
Cought an error! increasing counter!
This object handled 0 errors
Cought an error! increasing counter!
This object handled 0 errors
called by call_user_func:
Cought an error! increasing counter!
This object handled 1 errors
called manually
Cought an error! increasing counter!
This object handled 2 errors


------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=31798&edit=1

Reply via email to