Question:
How do I check if a function is being passed as a parameter?
Especially if the function being passed is returning a string etc?
IE in the example below I don’t want to htmlentities the function html being passed as a parameter, but I do want to htmlentities anything else. Also assuming there could be more than a single parameter that needs to take a function later.
Example:
function html($tag,$content)
{
if(!is_callable($content)){$var=htmlentities($var, ENT_NOQUOTES, "UTF-8");}
return "<".$tag.">".$content."</".$tag.">";
}
echo html(html('Example','Example'),'Example');
This example does not seem to work for me. I still get htmlentitied content when its a function.
While you cannot detect if a string being passed to a function is the result of another function, you can pass something that is not a string, but behaves like a string.
While I don’t really recommend this, and the side-effects could be plentiful, here is a possible solution.
A helper class that pretends to be a string, but it’s a class.
Your function. I’ve jiggled it around a bit, but it checks the type of the content.
Call your function as before.
So the function can now tell that you haven’t passed a string, but you can still use it exactly like a string inside the function.