I'm looking for the php equivalent of pythons %
operator.
# PYTHON Example
foo = 'variable_string'
baz = 'characters'
new_string = 'my %(foo)s of %(baz)s' % locals()
My Vague php context:
Note: This is to demonstrate why I want to do this, it does not reflect any code.
// PHP Example context
class Controller {
...
// Singles quotes used in intentionally
// This template string is meant to be overloadable.
public $template = '<h1>{$title}</h1><h2>{$subheading}</h2>';
....
}
class View {
...
public function render($template) {
$title = 'variable string';
$subheading = 'some other variable stuff';
// Example of the python operator
return magically_put_variables_in_string(
$template,
magically_get_named_variables_from_scope()
);
}
...
}
- Specifically I want the most canonical
magically_put_variables_in_string
implementation. - I cannot assume I will be able to use anything from later than php 5.3
- I do not mind passing an explicit
array('$variable_name' => $variable_name)
- Points for doing it without defining a new function.
Final Note: I have built a work around for my specific use case, however it does not satisfy the question.