#31798 [Bgs]: set_error_handler() object argument passed by value,not reference
| From: | vl409 at yandex dot ru | Date: | Wed, 13 Apr 2005 16:33:16 +0000 |
| Subject: | #31798 [Bgs]: set_error_handler() object argument passed by value,not reference | ||
| References: | 1 | Groups: | php.doc |
| Request: | Send a blank email to phpdoc+get-969367808@lists.php.net to get a copy of this message | ||
ID: 31798
User updated by: vl409 at yandex dot ru
Reported By: vl409 at yandex dot ru
Status: Bogus
Bug Type: Documentation problem
Operating System: linux-2.4.25,freebsd 4.10
PHP Version: 4.3.10
New Comment:
thank you for your work on php bugs - i already understood what
happens; i just think that if people understand documentation wrong in
some place, it`s worth to mention details in it.
in this case, it seems to me usefull to mention that in php4
set_error_handler() can`t be called in constructor, because of.. (link
on object creation docs)
again, thank you for your work and good luck =)
Previous Comments:
------------------------------------------------------------------------
[2005-03-15 14:39:37] vrana@php.net
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).
------------------------------------------------------------------------
[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") doesnt work in this
case.
am i right? =)
------------------------------------------------------------------------
[2005-02-01 22:13:42] sniper@php.net
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