note 58586 deleted from function.set-error-handler by aidan

From: Date: Wed, 09 Nov 2005 06:12:31 +0000
Subject: note 58586 deleted from function.set-error-handler by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-98288@lists.php.net to get a copy of this message
Note Submitter: phpfilenavigator at litoweb dot net Reason: useless ---- <?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.

« previous php.notes (#98288) next »