Re: exit in class constructor crashes
| From: | Daniel Convissor | Date: | Mon, 31 Jul 2000 02:26:40 +0000 |
| Subject: | Re: exit in class constructor crashes | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-9108@lists.php.net to get a copy of this message | ||
Hey Ho!
Daniel Convissor wrote:
>
> But, then the process isn't automatic anymore. The user needs to type in
> another line of code. If they don't use that line of code, the check doesn't
> happen. :(
Got an answer! Gee, I've had a lot of threads here, and elsewhere, in which
I'm having a dialogue with myself. Anyway...
Last week ended with this quagmire of not being able to exit before the end
of a class' constructor function. Very frustrating. So I was glad to go
visit my sweet heart. While we were hanging out, I had a flash of
inspiration.
The next day, we were passing the time telling stories. Figured, if she's
going to make a story teller out of me, I'll make a programmer out of her.
She was amenable.
So, today, she sits down at the controls, with I in the navigator's seat. We
stripped everything out of the class to make the experiment simple. The test
had a class and a constructor function. The function says hello and then
tries to exit(). As expected, BLAMO! PHP crashes.
Okay, now for the bright idea... I explain the purposes of brackets and
nesting. Then, at the point in the class where the exit() is supposed to
happen, we substitute an include file. This include contains a "}" and the
exit() in order to fake the function into thinking it reached the end of the
function, THEN calling the exit(). Okay, now the moment of truth... Flip
over to the web browser, hit the refresh button.... "YES!" I exclaim and
then jump out of my seat, jump up and down shouting "I am master of the
universe!"
Okay, at least MY universe :) None the less, this trick works! :)
Here's the test. It's broken up into two files, test.htm and fake.inc.
--------------------------------------------------------------
test.htm
----------------------
<?php
class ConstructorExit {
function ConstructorExit() {
echo "Welcome to the Constructor Exit Test.";
# exit();
# But, if you exit here, PHP crashes.
# So, include this fakeout...
include ("./fake.inc");
}# End of function ConstructorExit
}# End of class ConstructorExit
$Post = new ConstructorExit;
?>
-----------------------
fake.inc
-----------------------
<?php
}# End of function ConstructorExit
echo "<br>Well, you're now out of the function.";
echo "<br>Now, try exiting.";
exit();
echo "If you see this, the program didn't exit.";
?>
----------------------------------------------------------
Guess the only question is "will it work in PHP 4?" Hmm, maybe another
question is, is this workaround not needed in PHP 4 because one can exit()
from constructors?
Well, hope you like the fix and the story.
Enjoy,
--Dan
PS: Jen, your inspiration and support go a long way. Thanks!
--
T H E A N A L Y S I S A N D S O L U T I O N S C O M P A N Y
More than just answers. Solutions. (SM)
http://www.analysisandsolutions.com/
4015 7 Av #4, Brooklyn NY 11232 v: 718-854-0335 f: 718-854-0409