an indication that a script finished running

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jasongr
Forum Contributor
Posts: 206
Joined: Tue Jul 27, 2004 6:19 am

an indication that a script finished running

Post 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
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Re: an indication that a script finished running

Post 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.
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post 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 :P
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Re: an indication that a script finished running

Post 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 ?
jasongr
Forum Contributor
Posts: 206
Joined: Tue Jul 27, 2004 6:19 am

Post 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?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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.
jasongr
Forum Contributor
Posts: 206
Joined: Tue Jul 27, 2004 6:19 am

Post 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
Post Reply