<?php
class Composable {
    private $behaviors = array();
    
    public function __call($name, $arguments) {
        foreach ($this->behaviors as $behavior) {
            if ($behavior->provides($name)) {
                return $behavior->invoke($this, $name, $arguments);
            }
        }
        
        throw new Exception("No method $name and no behavior that implements it!");
    }
    
    public function attach($behavior) {
        $this->behaviors[] = $behavior;
    }
}

class User extends Composable {
}

abstract class Behavior {
    public function provides($name) {
        return method_exists($this, $name);
    }
    
    public function invoke($target, $name, $arguments) {
        array_unshift($arguments, $target);
        return call_user_func_array(array($this, $name), $arguments);
    }
}

class GrouppableBehavior extends Behavior {
    public function join(User $user, $groupName) {
        echo "The user has joined group $groupName.";
    }
}

$user1 = new User;
$user2 = new User;

$user1->attach(new GrouppableBehavior);
$user1->join('Test Group');
$user2->join('Test Group');
