Perform some actions in php

In your opinion, what is the best solution in PHP for providing a course of several php functions? For example, function A must return True to initiate function B, and function B must return True to initiate another function ...

Is there a system like Rollback / Commit in SQL to make sure in PHP?

Thanks.

+4
source share
5 answers

Running a chain of functions will look something like this:

try { if(!functionA()) { throw new Exception("Error message here", 100); // 100 can be any code, as long as it unique to the other throw exception codes } if(!functionB()) { throw new Exception("Error message here", 101); } if(!functionC()) { throw new Exception("Error message here", 102); } } catch(Exception $e) { /** * Do some actions depending on the error code of the exception (100, 101, 102) */ } 

But if I understand correctly, you want to perform a series of operations, and only if all of them are successful, you want them to be final, otherwise you want to cancel all operations?

In this case, it is difficult to say how you should realize this, because you are not saying what you want to achieve.

But I can give you some advice. Most operations that are not reversible have many possible checks. For example, if you want the file to be deleted from the file system, you can first check if the file is writable ( is_writable ) and even exists ( file_exists ). If you first complete all checks of all operations and then complete the operations, you can be sure that they will be completed successfully. Of course, you can forget the checks, or some things cannot be checked, but I do not see another option to fix this.

+2
source

There is no "rollback" available with php.

However, if you work with exceptions, you can try your code, and as soon as you get an exception, call the rollback function in the catch section.

Pseudocode:

 try { your_function_1(); // must throw exceptions on error your_function_2(); // must throw exceptions on error } catch(Exception $e){ your_rollback_code(); } 
+2
source

About “proactive functions” based on the results of other functions depends on the context of the functions. Several variants:

 if ( A() ) { if ( B() ) { anotherFunction(); } } 

or

 if ( A() && B() && anotherFunction() ) { } 

or

 if ( A() && B() ) { anotherFunction(); } 

In most cases, I would do the latter ( if ( a and b ) { do c } ).

+1
source

If you have many opportunities to run, it is best to use a state machine.

In the simplest case, this is just a loop, while the state variable does not have a done state. Each time one of the function reports is executed, you simply proceed to the next state.

A simple example:

 $nState = 0; while ($nState != 3) { switch ($nState) { case 0 : if (function1()) $nState = 1; break; case 1 : if (function2()) $nState = 2; break; case 2 : if (function3()) $nState = 3; break; } } 

An added benefit is that you can also return. Obviously, for clarity, you can make state constants, etc.

The state machine has the advantage that it keeps things neat and clear even with a large number of functions.

I assume that functions will not necessarily be successful on the first try, and sometimes you have to run them several times. If this is not so, then Stegemann’s answer is all the easier.

0
source

You can have an array of definitions:

 $callbacks = array ( array ( 'function' => 'function_1', 'rollback' => 'function_r1', 'arguments' => array(1), ), array ( 'function' => 'function_2', 'rollback' => 'function_r2', 'arguments' => array(1, 2), ), array ( 'function' => 'function_3', 'rollback' => 'function_r3', 'arguments' => array(1, 2, 3), ), ); 

And do something like this:

 $callbacks = array_values($callbacks); foreach ($callbacks as $key => $callback) { if (call_user_func_array($callback['function'], $callback['arguments']) !== true) { // failed, calling necessary rollbacks in reverse order foreach (array_reverse(array_slice($callbacks, 0, $key)) as $rollback) { if (is_callable($rollback['rollback']) === true) { call_user_func_array($rollback['rollback'], $rollback['arguments']); } } break; } // success } 
0
source

Source: https://habr.com/ru/post/1344062/


All Articles