- substr: documentation ( source)
- debug_backtrace: documentation ( source)
- array_shift: documentation ( source)
- lcfirst: documentation ( source)
<?php
class User
{
public function __call($name, $value)
{
$type = substr($name, 0, 3);
$variable = lcfirst(substr($name, 3));
if($type == 'get'){
return $this->$variable;
}
if($type == 'set'){
$this->$variable = $value[0];
return $this;
}
return call_user_func_array(array($this, $name), $value);
}
public function setEmail($email)
{
$trace=debug_backtrace();
$caller=array_shift($trace);
echo "Called by {$caller['function']}";
if (isset($caller['class'])) {
echo " in {$caller['class']}";
}
$this->email = $email;
return $this;
}
}
$user = new User();
$user->__call('setEmail', 'foo@bar.baz');
$user->setEmail('foo2@bar.baz');