php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #31798 set_error_handler() object argument passed by value,not reference
Submitted: 2005-02-01 21:55 UTC Modified: 2005-04-13 18:33 UTC
From: vl409 at yandex dot ru Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: 4.3.10 OS: linux-2.4.25,freebsd 4.10
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: vl409 at yandex dot ru
New email:
PHP Version: OS:

 

 [2005-02-01 21:55 UTC] 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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-02-01 22:13 UTC] sniper@php.net
reclassified

 [2005-02-02 16:31 UTC] 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-03-15 14:39 UTC] 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-04-13 18:33 UTC] vl409 at yandex dot ru
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 =)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Oct 05 22:01:26 2024 UTC