Created
February 4, 2012 15:52
-
-
Save swvitaliy/1738616 to your computer and use it in GitHub Desktop.
Example of automated fluent calls
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* fluent.php | |
* Пример автоматизации флаент вызовов с использованием магического метода __call. | |
* | |
* Author: swvitaliy | |
*/ | |
class F1 { | |
/** | |
* должен возвращать текущий инстанс, но не делает этого | |
* | |
*/ | |
public function aEcho () { echo "a\n"; } | |
/** | |
* должен возвращать инстанс класса F2 и не перекрываться флаент вызовом | |
* | |
*/ | |
public function getF2 () { | |
echo "getF2\n"; | |
return empty($this->f2) ? $this->f2 = new F2 : $this->f2; | |
} | |
} | |
class F2 { | |
public function bEcho () { return "b\n"; } | |
} | |
class _fluent_F1 extends F1 { | |
const _FLUENT_POSTFIX = 'And'; | |
public function _fluent_call_ ($name, $args, &$status) { | |
$status = false; | |
if (($k=strpos($name, self::_FLUENT_POSTFIX))===(strlen($name)-strlen(self::_FLUENT_POSTFIX)) && $k) { | |
if (method_exists($this, $m=substr($name, 0, $k))) { | |
$status = true; | |
return call_user_func_array(array($this, $m), $args); | |
} | |
} | |
} | |
public function __call ($name, $args) { | |
$status = false; | |
$this->_fluent_call_($name, $args, $status); | |
if ($status) | |
return $this; | |
// something else ... | |
} | |
} | |
//$f1 = new _fluent_F1; | |
//echo $f1->aEchoAnd()->getF2()->bEcho(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment