Error handling for PHP chain programming applications. Chained programming is very comfortable to use. This article attempts to implement a chained programming application in PHP. we know that after newclass, method is called, in conventional PHP programming, Php code chain programming is required for every call. This article attempts to implement a chain programming application in PHP.
We know that calling method after new class requires
Php code
$ Instance-> method1 ();
$ Instance-> method1 ();
$ Instance-> method1 ();
$ Instance-> method1 ();
In this way, there are endless N writes. if there is an error in the middle of the judgment, it will be like this.
Php code
If ($ instance-> method1 ())
If ($ instance-> method2 ())
$ Instance-> method3 ();
// Or
$ Instance-> method1 ();
If ($ instance-> hasError () die (error );
$ Instance-> method2 ();
If (....)...;
If ($ instance-> method1 ())
If ($ instance-> method2 ())
$ Instance-> method3 ();
// Or
$ Instance-> method1 ();
If ($ instance-> hasError () die (error );
$ Instance-> method2 ();
If (....)...;
It seems annoying and even more annoying to write, especially during the development period.
If this execution can be ensured
Php code
If ($ instance-> m0 ()-> m1 ()-> m2 ()-> hasError () die (error );
If ($ instance-> m0 ()-> m1 ()-> m2 ()-> hasError () die (error );
That's easy. to achieve this, the method is actually very simple. in this kind of method that can be chained, it contains error Judgments. In any case, this is returned, of course, methods like hasError do not return this. this type of method always appears at the end, but during the development period, we copy and paste N More
Php code
If ($ this-> hasError ())
Return $ this
// Someting ..
Return $ this;
If ($ this-> hasError ())
Return $ this
// Someting ..
Return $ this;
This is also annoying. of course, if it is finalized, it doesn't matter if you embed the code. The development phase is annoying.
You can use PHP magic to achieve this. Here we provide a basic structure.
Php code
Class CChain {
Private $ instance = null;
Private $ haserror = false;
Public function _ construct ($ instance ){
If (! Method_exists ($ instance, getError ))
Die (Instance does not have a method getError ().);
$ This-> instance = $ instance;
}
Public function _ call ($ m, $ ){
If ($ this-> haserror)
Return $ m = getError? $ This-> haserror: $ this;
$ This-> haserror = & $ this-> instance-> getError ()? : False;
If ($ this-> haserror)
Return $ m = getError? $ This-> haserror: $ this;
$ Ret = & call_user_func_array (array (& $ this-> instance, $ m), $ );
$ This-> haserror = & $ this-> instance-> getError ()? : False;
If ($ this-> haserror)
Return $ m = getError? $ This-> haserror: $ this;
If ($ m = getError) return $ this-> haserror;
If ($ ret = null)
Return $ this;
Return $ ret;
}
Public function _ get ($ n ){
Return $ this-> instance-> $ n;
}
Public function _ set ($ n, $ v ){
$ This-> instance-> $ n = $ v;
}
}
Class test {
Public $ error = false;
Public function getError (){
Return $ this-> error;
}
Public function setError ($ v ){
$ This-> error = $ v;
}
Public function m0 (){
/* Someting without return */
}
Public function m1 (){
/* Someting without return */
}
Public function m2 ($ foo = null ){
If ($ foo ){
Return $ this-> setError (error. _ METHOD __);
}
/* Someting without return */
}
}
$ Test = new CChain (new test );
Print_r ($ test-> m0 ()-> m1 ()-> m2 (1 ));
Echo ($ test-> error );
Callback we know that calling method after new class requires PHP code for every call in conventional Php programming...