You can use func_get_args()
and array_shift()
to isolate the constant string name.
[Codepad live]
<?php
class L {
const profile_tag_1 = 'Bla bla Age from %s to %s';
const profile_tag_2 = 'Wow Wow Age from %s to %s';
public static function __callStatic() {
$args = func_get_args();
$string = array_shift($args);
return vsprintf(constant('self::' . $string), $args);
}
}
L::__callStatic('profile_tag_1',12,12);
But, be aware when using this function with a generic call to a static method, you need to change __callStatic
signature to allow $name
and $arguments
like this:
class L {
const profile_tag_1 = 'Bla bla Age from %s to %s';
const profile_tag_2 = 'Wow Wow Age from %s to %s';
public static function __callStatic($name, $args) {
$string = array_shift($args);
return vsprintf(constant('self::' . $string), $args);
}
}
L::format('profile_tag_1',12,12);
A better way
Although, there is a better way to perform what you need (read Yoshi in comments), considering you are using everything static:
echo sprintf(L::profile_tag_1,12,14);
You don't even need a Class
at this point.