- substr: documentation ( source)
- strpos: documentation ( source)
- strrchr: documentation ( source)
- method_exists: documentation ( source)
<?php
function get_class_name($class)
{
if (is_object($class)) {
return get_class_name(get_class($class));
} else if (is_string($class)) {
if (strpos('\\', $class) !== false) {
return substr(strrchr($class, '\\'), 1);
}
return $class;
}
return false;
}
abstract class Visitor
{
public final function __call(string $method, array $args)
{
if ($method === 'visit') {
$method .= get_class_name(static::class);
die($method);
if (method_exists($this, $method)) {
return call_user_func_array([$this, $method], $args);
}
}
throw new Exception('Unknown method ' . $method);
}
}
interface Visitable
{
public function accept(Visitor $v);
}
class FooVisitor extends Visitor
{
public function visitFoo(Foo $foo)
{
print 'visit Foo' . PHP_EOL;
}
}
class Foo implements Visitable
{
public function accept(Visitor $v)
{
$v->visit($this);
}
}
$fv = new FooVisitor();
$f = new Foo();
$f->accept($fv);