Tuesday, December 16, 2008

Logging to DB after PHP is done generating content

Oh how I love register_shutdown_function of PHP. This bad baby will execute at the end of the scripts in call order. So, how is this useful?

Say you want to log some action, but that logging DB is on another server. Additionally you do not want to take into account that writing to the other server may break your transaction because of timeouts if logged in the middle of a transaction. Another use case: you do not want to change a bunch of code in different places to batch all logging routines up.

public function __construct($platform){
register_shutdown_function(array($this, 'afterProcess'));

public function afterProcess(){

foreach($this->getDataThatChange() as $key => $values){
$insert_data[] = $values;

$this->getDBClass()->DB_logserver_insert_query("LoggingTable", $insert_data, 'delayed');


The function afterProcess will batch all the changes and do a bulk insert into a myISAM table so it can used the DELAYED functionality.

This is done outside of all transactions, and at the end of the script as the data is returned.

But, why not just use __deconstructor() in PHP? I want it to happen before the deconstructor is called.


I have been using register_shutdown_function for years now, especially to clean up connections at the end of script execution. Since someone asked me if this was possible in PHP, I decided to post this quick usage of a cool php method.


Anonymous said...

>> I have been using register_shutdown_function for years now, especially to clean up connections at the end of script execution.

why it is needed to clean up connections at the end of script execution? for mysql, I suppose it is not needed

lsmith said...

as long as you dont use persistent connections. even still you might want to check if there are still open transactions, which would incicate issues in your code, that need to be fixed even if they are not going to do damage in the next request.

Martin Holzhauer said...

i also run in the __destuct problem with db access.
i wanted to write changes back to db, but alway got an error that my db object is already destructed.

i just workarrounded this with a save() method and i think this was the cleaner way, but i'm a programmer, im lazy so i will try to get this workign in my classes.

good post/information thx.

泡面 said...

俱樂部, 豆豆聊天室, 聊天室, 色情影片, 視訊聊天室, 免費視訊聊天, 免費視訊, 視訊美女, 視訊交友, 視訊聊天, 伊莉討論區, AV, 免費視訊聊天室, a片下載, aV, av片, A漫, av dvd, UT聊天室, 尋夢園聊天室, av成人網, 聊天室, 成人論壇, 正妹牆, 正妹百人斬, aio,伊莉, 本土自拍, 自拍, A片, 愛情公寓, 情色, 舊情人, 男同志聊天室, 色色網, ,嘟嘟情人色網, UT男同志聊天室, 情色貼圖, 情色文學, 情色交友, 色情聊天室, 色情小說, 成人聊天室, 成人小說, 一葉情貼圖片區, 情色小說, 日本A片, 成人網站, 色情, 色情遊戲, 情色視訊, 情色電影, 聊天室尋夢園, 080聊天室, aio交友愛情館, 色情a片, 一夜情, 辣妹視訊, 視訊聊天室, 免費視訊聊天, 情色論壇, 做愛, 做愛影片, av片, 色情網站, 免費視訊, 視訊, 視訊美女, 美女視訊, 女同志聊天室, 小高聊天室, 視訊交友, 視訊聊天, 免費視訊聊天室, 情人視訊網, 080苗栗人聊天室, 6K聊天室, 影音視訊聊天室, 視訊交友90739, 成人影片, 成人交友, 本土自拍, 美女交友, 情色聊天室, 寄情築園小遊戲, AV女優, A片下載,

泡面 said...

免費A片, ut聊天室, AV女優, 美女視訊, 免費成人影片, 成人論壇, 情色交友, 免費AV, 線上a片, 日本美女寫真集, 同志聊天室, 聊天室交友, 成人文章, 成人圖片區, 色情網站, 辣妹視訊, 美女交友, 微風成人區, 色美媚部落格, 色情影片, 成人影片, 成人網站, 免費A片, 上班族聊天室, A片,H漫, 18成人, a漫, av dvd, 一夜情聊天室, 微風成人, 成人圖片, 成人漫畫, 情色網, 日本A片, 免費A片下載, 性愛, 成人交友, 嘟嘟成人網, 嘟嘟成人網, 成人貼圖, 成人電影, 成人, 中部人聊天室, 080中部人聊天室, 成人貼圖, 成人小說, 成人文章, 成人圖片區, 免費成人影片, 成人遊戲, 微風成人, 愛情公寓, 成人電影, A片, 情色, 情色貼圖, 情色文學, 做愛, 成人遊戲, 成人影城, 色情聊天室, 色情小說, 一葉情貼圖片區, 情色小說, 色情, 寄情築園小遊戲, 色情遊戲, 成人網站, 麗的色遊戲, 色情網站, 成人論壇, 情色視訊, 情色電影, aio交友愛情館, 言情小說, 愛情小說, 色情A片, 情色論壇, 自拍, 癡漢, ,

Ashutosh said...


Hope you're doing great!

Our direct client is looking for a MySQL DBA for 6 months contract in Alpharetta, GA.

Please send me your resume along with billing rate and contact details, if you're interested.

Job Title: MySQL DBA
Job Location: Alpharetta, GA.
Duration: 6 months contract

Thanks and regards,
Ashutosh Baranwal
Primus Software Corporation
Tel: 678-935-3554
Fax: 678-336-1505