Created
November 16, 2015 18:22
-
-
Save beratdogan/05999bc513058439e06e to your computer and use it in GitHub Desktop.
This file contains 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 | |
function partial() { | |
$args = func_get_args(); | |
return function() use($args) { | |
return call_user_func_array('call_user_func', array_merge($args, func_get_args())); | |
}; | |
} | |
function with() | |
{ | |
$args = func_get_args(); | |
$closure = array_pop($args); | |
$call_enter = partial('call_user_method', '_enter'); | |
$call_exit = partial('call_user_method', '_exit'); | |
try { | |
array_map($call_enter, $args); | |
call_user_func_array($closure, $args); | |
} | |
finally { | |
array_map($call_exit, $args); | |
} | |
} | |
interface ContextManager | |
{ | |
public function _enter(); | |
public function _exit(); | |
} | |
class QWEQWE implements ContextManager | |
{ | |
public function _enter() | |
{ | |
// do something | |
} | |
public function _exit($exc_type, $exc_value, $traceback) | |
{ | |
// do something | |
} | |
} | |
$qwe1 = new QWEQWE() | |
$qwe2 = new QWEQWE() | |
with($qwe1, $qwe2, function($qweqwe) | |
{ | |
// do something | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment