Once i initiated a function i am setting a flag on DB to TRUE.
and i need to set it FALSE on the end of the function.
Ex:
Class Preprocessor extends Machine{
function process(){
$this->db->setValue(TRUE); //Setting DB flag to TRUE
//OTHER CODES
$this->close_db_value(); // Setting DB flag to FALSE
}
function close_db_value(){
$this->db->setValue(FALSE); //Setting DB flag to FALSE
}
}
As you can see that it will work in normal cases, But if some error encountered in //OTHER CODES section then it will not execute the rest of codes.
The function mainly work in background (But not as a command line,its just closing the connection and start executing on background).
How can i ensure that the close_db_value() function executed upon script termination ?
Some of possibilities
Some critical errors that which leads the script to termination
exit called somewhere on the //OTHER CODE section
PHP max_execution_time exceeded
A Force Kill
Please help.
Use the set_error_handler() function in PHP. Place the name of the callback function you want called in case of error as the first parameter. Put the level of error (optional) that you want as the trigger as the second parameter.
If you are looking to handle every other type of error (except SIGKILLs or force quits), you can also try using register_shutdown_function() with a error_get_last() and a switch-case statement with the various types of errors you wish to handle (E_NOTICE, E_DEPRECATED, etc.).
For example,
register_shutdown_function('shutdown_callback');
function shutdown_callback(){
$error_type = error_get_last();
switch($error_type){
case E_ERROR: break;
case E_WARNING: break;
}
}
The __destruct(); magic function will also execute every time the script ends, or an object is unloaded.
Once i initiated a function i am setting a flag on DB to TRUE.
and i need to set it FALSE on the end of the function.
Ex:
Class Preprocessor extends Machine{
function process(){
$this->db->setValue(TRUE); //Setting DB flag to TRUE
//OTHER CODES
$this->close_db_value(); // Setting DB flag to FALSE
}
function close_db_value(){
$this->db->setValue(FALSE); //Setting DB flag to FALSE
}
}
As you can see that it will work in normal cases, But if some error encountered in //OTHER CODES section then it will not execute the rest of codes.
The function mainly work in background (But not as a command line,its just closing the connection and start executing on background).
How can i ensure that the close_db_value() function executed upon script termination ?
Some of possibilities
Some critical errors that which leads the script to termination
exit called somewhere on the //OTHER CODE section
PHP max_execution_time exceeded
A Force Kill
Please help.
Use the set_error_handler() function in PHP. Place the name of the callback function you want called in case of error as the first parameter. Put the level of error (optional) that you want as the trigger as the second parameter.
If you are looking to handle every other type of error (except SIGKILLs or force quits), you can also try using register_shutdown_function() with a error_get_last() and a switch-case statement with the various types of errors you wish to handle (E_NOTICE, E_DEPRECATED, etc.).
For example,
register_shutdown_function('shutdown_callback');
function shutdown_callback(){
$error_type = error_get_last();
switch($error_type){
case E_ERROR: break;
case E_WARNING: break;
}
}
The __destruct(); magic function will also execute every time the script ends, or an object is unloaded.
As you probably already know, the error handler in php can be replaced by a function using
set_error_handler()
I'm making my own error handler, and I want to do something when I have gotten all of the errors.
As of right now, the custom error handling function will be executed every time an error occurs.
So, what a way to know if it's the last error, so I can execute a html script.
I suspect you are looking for register_shutdown_function():
Registers a callback to be executed after script execution finishes or
exit() is called.
But you'd better assure that your callback function does not trigger any error xD
If you want to handle all the errors in one go (which means you will lose visibility of errors occuring before a fatal error - hence I do not recomend this) then register a sutdown function and just accumulate the data via your error handler then in your shutdown function report the errors....
<?php
$mylog="started\n"
function logit() {
global $mylog;
if ($mylog) {
file_put_contents(tmpnam('/var/log/php', 'err');
}
}
register_shutdown_function('logit');
function whoops()
{
global $mylog;
$mylog.="something bad happenned\n";
}
set_error_handler('whoops');
....
(this can all be done with an auto-prepend file)
I have methods which parameters can only handle certain types of variable. I have a few ideas on how to validate these type and I need your help to choose the best way.
I could:
Just return false if one of the types of variable is wrong, without letting the user know what's happening. Plus, if the function's output isn't usually checked by the user--e.g. ob_start()--they won't know it's even wrong;
Throw a custom InvalidArgumentException saying "Type of parameter X is incorrect." Thus I have to check every single parameter, making the code unreadable. Plus, the exception actually has to be catched, and I dislike these try...catch in my code.
Call error_log(). But yet I have to check every single parameter.
What option would you choose? Why? Otherwise, do you have a better idea?
UPDATE
When I talk about types, I mean these:
http://php.net/manual/en/language.types.php
The only way to do type checking in php is using the built-in function. You can find a list here : http://www.php.net/manual/en/ref.var.php It's a real pain in the ass, but you have no choice.
For the checking itself, I'll check all the parameters type at the beginning of the function and throw an error if not. Then you can always add some debuging print_r to discover the culprit.
If you are talking to arguments passed in your code of course you MUST log errors and fix every possibile mistakes in your code.
Example:
function(){
if (!is_array($arg))
trigger_error();
}
OR
Using only objects you can specify the type of arguments
Example:
function yay(Class1 $arg1, Class2 $arg2){
//That's it!
}
I'm kind of interested in getting some feedback about this technique I picked up from somewhere.
I use this when a function can either succeed or fail, but you'd like to get more information about why it failed. A standard way to do this same thing would be with exception handling, but I often find it a bit over the top for this sort of thing, plus PHP4 does not offer this.
Basically the technique involves returning true for success, and something which equates to false for failure. Here's an example to show what I mean:
define ('DUPLICATE_USERNAME', false);
define ('DATABASE_ERROR', 0);
define ('INSUFFICIENT_DETAILS', 0.0);
define ('OK', true);
function createUser($username) {
// create the user and return the appropriate constant from the above
}
The beauty of this is that in your calling code, if you don't care WHY the user creation failed, you can write simple and readable code:
if (createUser('fred')) {
// yay, it worked!
} else {
// aww, it didn't work.
}
If you particularly want to check why it didn't work (for logging, display to the user, or do whatever), use identity comparison with ===
$status = createUser('fred');
if ($status) {
// yay, it worked!
} else if ($status === DUPLICATE_USERNAME) {
// tell the user about it and get them to try again.
} else {
// aww, it didn't work. log it and show a generic error message? whatever.
}
The way I see it, the benefits of this are that it is a normal expectation that a successful execution of a function like that would return true, and failure return false.
The downside is that you can only have 7 "error" return values: false, 0, 0.0, "0", null, "", and (object) null. If you forget to use identity checking you could get your program flow all wrong. Someone else has told me that using constants like an enum where they all equate to false is "ick".
So, to restate the question: how acceptable is a practise like this? Would you recommend a different way to achieve the same thing?
I agree with the others who have stated that this is a little on the WTFy side. If it's clearly documented functionality, then it's less of an issue, but I think it'd be safer to take an alternate route of returning 0 for success and integers for error codes. If you don't like that idea or the idea of a global last error variable, consider redefining your function as:
function createUser($username, &$error)
Then you can use:
if (createUser('fred', $error)) {
echo 'success';
}
else {
echo $error;
}
Inside createUser, just populate $error with any error you encounter and it'll be accessible outside of the function scope due to the reference.
As long as it's documented and contracted, and not too WTFy, then there shouldn't be a problem.
Then again, I would recommend using exceptions for something like this. It makes more sense. If you can use PHP5, then that would be the way to go. Otherwise you don't have much choice.
A more common approach I have seen when exceptions aren't available is to store the error type in a 'last_error' variable somewhere and then when a failure happens (ie it returns false) look up the error.
Another approach is to use the venerable unix tool approach numbered error codes - return 0 for success and any integer (that maps to some error) for the various error conditions.
Most of these suffer in comparison to exceptions when I've seen them used however.
Just to respond to Andrew's comment -
I agree that the last_error should not be a global and perhaps the 'somewhere' in my answer was a little vague - other people have suggested better places already so I won't bother to repeat them
how acceptable is a practice like this?
I'd say it's unacceptable.
Requires the === operator, which is very dangerous. If the user used ==, it leads to a very hard to find bug.
Using "0" and "" to denote false may change in future PHP versions. Plus in a lot of other languages "0" and "" does not evaluate to false which leads to great confusion
Using getLastError() type of global function is probably the best practice in PHP because it ties in well with the language, since PHP is still mostly a procedural langauge. I think another problem with the approach you just gave is that very few other systems work like that. The programmer has to learn this way of error checking which is the source of errors. It's best to make things work like how most people expect.
if ( makeClient() )
{ // happy scenario goes here }
else
{
// error handling all goes inside this block
switch ( getMakeClientError() )
{ case: // .. }
}
Often you will return 0 to indicate success, and 1, 2, 3, etc. to indicate different failures. Your way of doing it is kind of hackish, because you can only have so many errors, and this kind of coding will bite you sooner or later.
I like defining a struct/object that includes a Boolean to indicate success, and an error message or other value indicate what kind of error occurred. You can also include other fields to indicate what kind of action was executed.
This makes logging very easy, since you can then just pass the status-struct into the logger, and it will then insert the appropriate log entry.
Reinventing the wheel here. Using squares.
OK, you don't have exceptions in PHP 4. Welcome in the year 1982, take a look at C.
You can have error codes. Consider negative values, they seem more intuitive, so you would just have to check if (createUser() > 0).
You can have an error log if you want, with error messages (or just arbitrary error codes) pushed onto an array, dealt with elegance afterwards.
But PHP is a loosely typed language for a reason, and throwing error codes that have different types but evaluate to the same "false" is something that shouldn't be done.
What happens when you run out of built-in types?
What happens when you get a new coder and have to explain how this thing works? Say, in 6 months, you won't remember.
Is PHP === operator fast enough to get through it? Is it faster than error codes? or any other method?
Just drop it.
When exceptions aren't available, I'd use the PEAR model and provide isError() functionality in all your classes.
Ick.
In Unix pre-exception this is done with errno. You return 0 for success or -1 for failure, then you have a value you can retrieve with an integer error code to get the actual error. This works in all cases, because you don't have a (realistic) limit to the number of error codes. INT_MAX is certainly more than 7, and you don't have to worry about the type (errno).
I vote against the solution proposed in the question.
It does make sense that a successful execution returns true. Handling generic errors will be much easier:
if (!createUser($username)) {
// the dingo ate my user.
// deal with it.
}
But it doesn't make sense at all to associate meaning with different types of false. False should mean one thing and one thing only, regardless of the type or how the programming language treats it. If you're going to define error status constants anyway, better stick with switch/case
define(DUPLICATE_USERNAME, 4)
define(USERNAME_NOT_ALPHANUM, 8)
switch ($status) {
case DUPLICATE_USERNAME:
// sorry hun, there's someone else
break;
case USERNAME_NOT_ALPHANUM:
break;
default:
// yay, it worked
}
Also with this technique, you'll be able to bitwise AND and OR status messages, so you can return status messages that carry more than one meaning like DUPLICATE_USERNAME & USERNAME_NOT_ALPHANUM and treat it appropriately. This isn't always a good idea, it depends on how you use it.
I like the way COM can handle both exception and non-exception capable callers. The example below show how a HRESULT is tested and an exception is thrown in case of failure. (usually autogenerated in tli files)
inline _bstr_t IMyClass::GetName ( ) {
BSTR _result;
HRESULT _hr = get_name(&_result);
if (FAILED(_hr)) _com_issue_errorex(_hr, this, __uuidof(this));
return _bstr_t(_result, false);
}
Using return values will affect readability by having error handling scattered and worst case, the return values are never checked by the code. That's why I prefer exception when a contract is breached.
If you really want to do this kind of thing, you should have different values for each error, and check for success. Something like
define ('OK', 0);
define ('DUPLICATE_USERNAME', 1);
define ('DATABASE_ERROR', 2);
define ('INSUFFICIENT_DETAILS', 3);
And check:
if (createUser('fred') == OK) {
//OK
}
else {
//Fail
}
Other ways include exceptions:
throw new Validation_Exception_SQLDuplicate("There's someone else, hun");),
returning structures,
return new Result($status, $stuff);
if ($result->status == 0) {
$stuff = $result->data;
}
else {
die('Oh hell');
}
I would hate to be the person who came after you for using the code pattern you suggested originally.
And I mean "Came after you" as in "followed you in employment and had to maintain the code" rather than "came after you" "with a wedgiematic", though both are options.
In my opinion, you should use this technique only if failure is a "normal part of operation" of your method / function. For example, it's as probable that a call suceeds as that it fails. If failure is a exceptional event, then you should use exception handling so your program can terminate as early and gracefully as possible.
As for your use of different "false" values, I'd better return an instance of a custom "Result"-class with an proper error code. Something like:
class Result
{
var $_result;
var $_errormsg;
function Result($res, $error)
{
$this->_result = $res;
$ths->_errorMsg = $error
}
function getResult()
{
return $this->_result;
}
function isError()
{
return ! ((boolean) $this->_result);
}
function getErrorMessage()
{
return $this->_errorMsg;
}
Look at COM HRESULT for a correct way to do it.
But exceptions are generally better.
Update: the correct way is: define as many error values as you want, not only "false" ones. Use function succeeded() to check if function succeeded.
if (succeeded(result = MyFunction()))
...
else
...