It is possible, but if you will do it, it will mean that you are really weird person.
According to this answer - reconstruct/get code of php function
you may get php function code as string something like this:
$a = function () {
echo "hello";
};
$func = new ReflectionFunction($a);
$filename = $func->getFileName();
$start_line = $func->getStartLine() - 1; // it's actually - 1, otherwise you wont get the function() block
$end_line = $func->getEndLine();
$length = $end_line - $start_line;
$source = file($filename);
$body = implode("", array_slice($source, $start_line, $length));
print_r($body);
After you will got function code as string, you will be able to send it to javascript as string, write your own php parser (with blackjack and whores), parse your function and according to your results transform function into js function, and then using eval
construction do your weird stub.
But if you will look on your problem on other angle, you will see, that no need to this it. Most easiest way is use decorator or facade on javascript side:
function MyCoolObjectDecorator(data) {
this.data = data;
}
MyCoolObjectDecorator.prototype.formatter = function () {
return this.data;
}
[send ajax request].then(function(response) {
return new MyCoolObjectDecorator(response).
}).then(function(response){
console.log(response.formatter());
})
Using this approach you will use your backend as data layer, and your frontend as UI layer, instead of making data-representation mess.