Chained programming is very comfortable to use, this article attempts to implement a chain-based programming application under PHP
We know that the method is called after the new class, and every call in regular PHP programming is
PHP code
$instance->method1 ();
$instance->method1 ();
$instance->method1 ();
$instance->method1 ();
So the endless write n more, if there is a mistake in the middle that's it.
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 annoying to write, especially during the development period, which is a nightmare.
If you can guarantee that this is done
PHP code
if ($instance->m0 ()->m1 ()->m2 ()->haserror ()) Die (error);
if ($instance->m0 ()->m1 ()->m2 ()->haserror ()) Die (error);
That is easy, to achieve this, the method is very simple is in this can be chained to the method contains the error judgment, anyway, return this, of course, similar to the Haserror method is not to return this, the kind of method always appears in the last, but in the development period, We copy and paste N-Multiple in the method
PHP code
if ($this->haserror ())
Return $this
Someting.
return $this;
if ($this->haserror ())
Return $this
Someting.
return $this;
This is annoying enough, of course, if stereotypes, then embed the code does not matter. The development period is annoying to the dead.
You can use the Magic method of PHP to achieve this, here is 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 has a method GetError ().);
$this->instance= $instance;
}
Public Function __call ($m, $a) {
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), $a);
$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);
http://www.bkjia.com/PHPjc/478767.html www.bkjia.com true http://www.bkjia.com/PHPjc/478767.html techarticle chained programming is very comfortable to use, this article attempts to implement a chain programming under PHP application we know that after the new class call method, in general PHP programming every call to PHP code ...