Closed qswks closed 12 years ago
In my opinion, there are a couple of things we could do in this situation.
We could use the @
operator to surpress the error (thus giving us a blank string) or we could totally ignore the object, like above.
$template = $this->_parse_single($key, (string)$val, $template);
Alternatively, we could use http_build_query()
, json_encode()
or serialize()
to provide a readable string representation of the object if we so desired. Or we could just add an error message to the class to prevent objects from being passed?
You shouldn't exclude objects entirely - the __toString()
functionality would make them an acceptable replacement value for a parsed pseudo-variable. Personally, I think throwing the error is correct - an object should not be an acceptable replacement unless it has a __toString()
capability.
The Parser class is designed to handle string replacements, and that's it. It doesn't tout any additional features. If someone needs something fancier, they should use one of the template libraries out there.
@cryode is correct - this is only supposed to work with strings.
Now, there is no suitable answer yet..Yeah?
@Myerden If you want to use objects in the parser, the objects must implement the __toString()
magic method. That, or make sure to manually convert your object into a string when defining it as a replacement value.
When you pass an object as $data to the Tempalte Parser Class an error is thrown for each object that couldn't be converted to string.
This snipplet:
Throws these errors:
Suggested patch (system/libraries/Parser.php - Line 101)