Skip to content

Instantly share code, notes, and snippets.

@travisfont
Last active August 29, 2015 14:01
Show Gist options
  • Save travisfont/8b3942650a9102f0a6de to your computer and use it in GitHub Desktop.
Save travisfont/8b3942650a9102f0a6de to your computer and use it in GitHub Desktop.
New observer pattern (test 1)
<?php
interface imethods
{
public function add($x, $y);
public function sub($x, $y);
}
class methods
{
protected function _add($args)
{
list($num1, $num2) = $args;
$answer = ($num1 + $num2);
echo $num1.' + '.$num2.' = '.$answer;
return $answer;
}
protected function _sub($args)
{
list($num1, $num2) = $args;
$answer = ($num1 - $num2);
echo $num1.' - '.$num2.' = '.$answer;
return $answer;
}
}
final class Calculator extends methods implements imethods
{
public function add($x, $y)
{
return $this->__call(__FUNCTION__, func_get_args());
}
public function sub($x, $y)
{
return $this->__call(__FUNCTION__, func_get_args());
}
public function __call($method, $args)
{
self::log($method, $args, $this->{'_'.$method}($args));
}
static private function log($method, $args, $return = false)
{
$data = array
(
'method' => $method,
'args' => $args,
'return' => $return
);
// log function here
var_dump($data);
}
}
$enter = new Calculator();
$enter->add(10, 20);
$enter->sub(15, 40);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment