Page 1 of 1
an indication that a script finished running
Posted: Wed Oct 13, 2004 3:12 pm
by jasongr
Hello
I was wondering if there is some construct in PHP that will allow me to know that the script has finished running.
I would like to perform some cleanup code as soon as the script finishes running. e.g. database flushing.
I know that I can invoke my code at the very end of my script but I am looking for a more robust solution.
If I depend on programmers to remember to include some code at the end of their scripts I bound to have 80% of them forgetting to do it.
I am looking for a construct similar to a desctructor.
I am using PHP 5.0
any insight would be appreciated
Re: an indication that a script finished running
Posted: Wed Oct 13, 2004 4:36 pm
by Roja
jasongr wrote:
If I depend on programmers to remember to include some code at the end of their scripts I bound to have 80% of them forgetting to do it.
Simple enough - make their script not run without including it.
As a header, put a ob_start() in, (say, to do utf-8 encoding, or compression, or similar).
Then in the ("required") footer, put the closing ob_end_flush.
That way, if they dont include the footer, THEY GET NO OUTPUT - obviously an error in their code.
If you find some numbskull using his OWN ob_end_flush, make him stop - should be the exception to the rule, not the rule itself.
Posted: Wed Oct 13, 2004 4:49 pm
by kettle_drum
Since your using php 5 you can use the __destruct() function in your classes. If your not using OOP then maybe take this as a reason to start using it

Re: an indication that a script finished running
Posted: Thu Oct 14, 2004 3:19 am
by timvw
jasongr wrote:
I know that I can invoke my code at the very end of my script but I am looking for a more robust solution.
If I depend on programmers to remember to include some code at the end of their scripts I bound to have 80% of them forgetting to do it.
what about
http://be2.php.net/manual/en/ini.sect.d ... ppend-file ?
Posted: Thu Oct 14, 2004 3:22 am
by jasongr
I wonder if I can count on the destructor.
For example, I have 2 classes:
class DatabaseManagaer which provides an API for interating with the database and class A which uses it
I want class A to do some cleanup code.
can I count that the DatabaeManager object will still be alive when the destructor of class A is invoked?
Posted: Thu Oct 14, 2004 3:29 am
by timvw
from the manual:
PHP 5 introduces a destructor concept similar to that of other object-oriented languages, such as C++. The destructor method will be called as soon as all references to a particular object are removed or when the object is explicitly destroyed.
So, if you have a reference to a databasemgr instance in your class A, it will still exist when the destructor of A is called.
Posted: Thu Oct 14, 2004 3:31 am
by jasongr
in fact, if I make my database manager a singleton, then it will outlive all the other classes instances between it will hold a reference to itself