Skip to content

Instantly share code, notes, and snippets.

@beratdogan
Created November 16, 2015 18:22
Show Gist options
  • Save beratdogan/05999bc513058439e06e to your computer and use it in GitHub Desktop.
Save beratdogan/05999bc513058439e06e to your computer and use it in GitHub Desktop.
<?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