views:

42

answers:

1

I'm trying to extend my ActiveRecord class with some dynamic methods. I would like to be able to run this from my controller

$user = User::find_by_username(param);
$user = User::find_by_email(param);

I've read a little about overloading and think that's the key. I'v got a static $_attributes in my AR class and I get the table name by pluralizing my model (User = users) in this case.

How do I do this? All models extends the ActiveRecord class.

+2  A: 

You have to use the __callStatic() magic method, which is available as PHP5.3

public static function __callStatic($name, $arguments) {
/*
    Use strpos to see if $name begins with 'find_by'
    If so, use strstr to get everything after 'find_by_'
    call_user_func_array to regular find method with found part and $arguments
    return result
*/
}
Gordon
Great! PHP 5.3 have really nice features :)
sandelius