note 58586 added to function.set-error-handler

From: Date: Tue, 08 Nov 2005 22:21:38 +0000
Subject: note 58586 added to function.set-error-handler
Groups: php.notes 
Request: Send a blank email to php-notes+get-98278@lists.php.net to get a copy of this message
<?php class error { var $error; function error() { $this->setIni(); } function handler() { echo $this->error.'!!'; } function setText($text) { $this->error = $text; } function setIni() { set_error_handler(array(&$this, 'handler')); // The '&' operator doesn't have any effect } } $eh = new error; $eh->setText('Error! <br>'); // Will not get set trigger_error('text', E_USER_ERROR); // prints '!!' ?> This code have an important error. You never must pass a reference to $this in the function that load the class. That's why don't work the reference &. The class isn't yet charged and I'm using the reference to it. Then pass a copy and not the reference. The correct code is: <?php class error { var $error; function handler() { echo $this->error.'!!'; } function setText($text) { $this->error = $text; } function setIni() { set_error_handler(array(&$this, 'handler')); // Now the class is now charged and works the & } } $eh = new error; $eh->setIni(); $eh->setText('Error! <br>'); // Will not get set trigger_error('text', E_USER_ERROR); // prints '!!' ?> Once time is class loaded, I initialize it. ---- Manual Page -- http://www.php.net/manual/en/function.set-error-handler.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+58586 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+58586&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+58586&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+58586&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+58586&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+58586&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+58586&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+58586&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+58586&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#98278) next »