473,323 Members | 1,570 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

register_shutdown_function only once

code green
1,726 Expert 1GB
I only want to register and execute once my shutdown function.
How do I do the equivalent of this psuedo code
Expand|Select|Wrap|Line Numbers
  1. if(!is_registered(my_shutdown_func)
  2.  register_shutdown_func(my_shutdown_func)
May 6 '09 #1
7 3700
Dormilich
8,658 Expert Mod 8TB
the only thing I can think of right now is adding the function name in a Registry Pattern object, so you can check there whether a function has already been registered somewhere.

if you're using objects anyway, consider using __destruct() (as of PHP 5)
May 6 '09 #2
code green
1,726 Expert 1GB
I use shutdownFunc() function to cleanly end a script, that is:
collect error messages into an error object from all classes and call the destructor.
It is particularly powerful when developing because class errors will always be output.
__destruct may not be called if the script crashes.

I am just trying to save the overhead of checking if an object still exists before calling destruct and getting double messages
May 7 '09 #3
Dormilich
8,658 Expert Mod 8TB
@code green
chances are that shutdownFunc() also won't work in that case.

@code green
if the object doesn't exist its __destruct() method won't be executed. to prevent double error messages you could define the sending procedure in the error object's __destruct() method.
May 7 '09 #4
code green
1,726 Expert 1GB
Thanks for the suggestions
chances are that shutdownFunc() also won't work in that case.
Seems to work if php or MySql or MsSql encounter a critical error
or time out.
But I am using Apache Windows where register_shutdown behaves differently.
if the object doesn't exist its __destruct() method won't be executed.
But calling the function
Expand|Select|Wrap|Line Numbers
  1. $object->returnErrorMessages(); 
is fatal.

Is there a way I can use __destruct() to pass error information out of, say
a MySql class into an error object class.
At the moment I handle it like so
Expand|Select|Wrap|Line Numbers
  1. $errorobj->collecterrs($mysqlobj->returnErrorMessages()) ; 
  2.  $mysqlobj->--destruct();
May 7 '09 #5
Dormilich
8,658 Expert Mod 8TB
@code green
then there's something wrong with the call, like e.g. you try to call a private method publicly.

@code green
the best way would probably be using Exceptions. when you catch those, pass the info given by the Exception to the Error class. (ex. 1)

generally, it's not very practical to hard-link the error class into the user class (because of the dependency). a workaround would be passing the error object while instantiating the user class (but I doubt the practicability) (ex. 2)

you may also experiment with the Observer Pattern.

ex. 1
Expand|Select|Wrap|Line Numbers
  1. try
  2. {
  3.     $mysql->connect();
  4.     // do other stuff with $mysql
  5. }
  6. catch (Exception $e) // or any other suitable Exception class
  7. {
  8.     // the addException() method extracts all the info* from your Exception
  9.     // into the error logging class. Errors is set up here as Abstract Registry
  10.     Errors::addException($e);
  11.  
  12.     // execute fallback code
  13. }
  14.  
  15. * including, file name, line number, error code, error message and backtrace by default,
  16.   you can use magic constants (__METHOD__, …) to get even more info
note: you can use set_error_handler() to convert errors (below E_ERROR level) into Exceptions.

ex. 2
Expand|Select|Wrap|Line Numbers
  1. $errorobj = new Errors;
  2. $mysql = new MysqlHandler($errorobj); // looks…, is very ugly
  3. // or
  4. $mysql->addErrorCollector($errorobj); // questionable approach for a DB class
  5.  
  6. class MysqlHandler
  7. {
  8.   …
  9.   function __destruct()
  10.   {
  11.     if ($err = $this->returnErrorMessages())
  12.       $this->errorCollector->collecterrs($err);
  13.   }
  14. }
@code green
you shouldn't call __destruct() explicitly like that, because the object is still "living". it is called if the object is either unset (unset($mysqlobj);) or destroyed at script end.

[EDIT]
more Patterns here
May 7 '09 #6
code green
1,726 Expert 1GB
$object->returnErrorMessages(); is fatal
It is fatal if $object no longer exists!
As the last of our remote servers were only recently upgraded
from PHP 4 to 5, I have legacy php4 still being ported, hence
you shouldn't call __destruct() explicitly like that
I changed the destructor to __destruct() from destructMethod() but not yet changed the calls.

It is the same with exceptions. Only partly ported error trapping to this style.

I agree with not hard-linking error objects to other class objects.
That is why I am not passing them as a parameter or using global.

I imagine when I am fully using exceptions and __construct() __destruct()
that some of my inbuilt problems will disappear
Thanks for the help.
May 7 '09 #7
Dormilich
8,658 Expert Mod 8TB
@code green
definitely. post back if new questions arise then.

kind regards, Dormi

PS: it took me half a year to port my application from PHP 4 to 5 (ok, considered that I made it in my spare time…)
May 7 '09 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: | last post by:
Hi all ! I am using register_shutdown_function. It behaves something strange. register_shutdown_function("f1"); register_shutdown_function("f2"); they fire in sequence f1
2
by: Joshua Beall | last post by:
Hi All, I use register_shutdown_function to register a function that then gets run when the script finishes executing. It worked great before, but sometime last week on my development server...
3
by: Derek Battams | last post by:
I want to ensure that I'm releasing a lock on a file after _all_ objects have been destroyed. What I'm trying to do is use register_shutdown_function to call a function that releases the lock on...
1
by: yawnmoth | last post by:
Say I have the following PHP script: <? register_shutdown_function('test'); ob_start(); echo "part2"; function test() { $output = ob_get_contents();
1
by: Stuart A Yeates | last post by:
I'm trying to write a template that matches "*" but which has an if clause which if only executed once per type of element seen (where all elements of a type have the same name and namespace). The...
1
by: Jacob Jensen | last post by:
Hi I have a problem creating C++ code that is multiplatform compilable. My problem is using the "#pragma once" directive which can be used by microsoft Visual Studio pre-compiler but which gives...
2
by: alex bazan | last post by:
I came across this function as i got a problem with very long-running scripts which use transactions... in the php manual i do not see clearly stated if the function defined will be executed if...
29
by: Neil | last post by:
Running a SQL 7 system on a Windows 2000 server using Access 2000 on client machines as a front end. System administrator currently reboots the server once a month. Yesterday we had some weird...
26
by: Rick | last post by:
I'm told that "#pragma once" has made it into the ISO standard for either C or C++. I can't find any reference to that anywhere. If it's true, do any of you have a reference I can use? ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.