Reconstruct/Get Source Code of a PHP Function

Reconstruct / get source code of a PHP function

Expanding on the suggestion to use the ReflectionFunction, you could use something like this:

$func = new ReflectionFunction('myfunction');
$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);

Refactoring: How to replace function call with function code

Use Refactor | Inline... in PhpStorm (invoke it on a function/method definition).

<?php
function aa($hello)
{
return "Hello $hello";
}

echo aa('Yo!');

Sample Image

Final result for that simple code:

<?php

echo "Hello 'Yo!'";

As you may see it's a bit incorrect (the single quotes around the variable content), so make sure to check your code afterwards.


Final result for your code sample (after using it on each function):

Sample Image

HHVM possible to recover source code from authoritative repo?

Yes, it is possible to disassemble HHVM's bytecode repository and reconstruct something close the original source. While HHVM does not provide any tools for this at present, HipHop bytecode (HHBC) is pretty close to the original source and contains rich metadata that includes local variable names, function names, etc. In that respect, HHBC bears some similarity to Java's bytecode or .NET's IL.

It might be possible to strip some of this metadata, but a lot of it is needed to handle stuff like "$f(..)", "call_user_func(..)", "class_exists(..)", and "$$x", not to mention the reflection APIs (ReflectionClass, ReflectionFunction, etc).

You might want to try one of the many PHP->PHP obfuscators out there (disclaimer: I haven't tried any of these obfuscators). Some of the better PHP->PHP obfuscators attempt to detect if your code uses a function name or class name in a "dynamic" way and try to avoid renaming these classes or functions, but I would imagine there may be some corner cases where these heuristics fail and some amount of manual tuning or adjustments are required.

Also, depending on your situation, it might be possible to use file system permissions to solve your problem (i.e. prevent regular users on your server from being able to access the bytecode repository), though it sounds like this might be outside of your control for your use case.

Is there any way to response anonymous function with stdClass from php to javascript

No. There is no way to pass that function in Javascript because not only PHP runs on server side and Javascript will run on client side, but also that function will be parsed into function object by the time you get that object $obj.

The output of var_dump($obj) is

object(stdClass)#1 (1) {
["xAxis"]=>
object(stdClass)#2 (2) {
["allowDecimals"]=>
bool(false)
["labels"]=>
object(stdClass)#3 (1) {
["formatter"]=>
object(Closure)#4 (0) {
}
}
}
}

You can see that the function is parsed into an object and the end output that you might will get won't have that function at all. You will get rest of the params and values, though.

{"xAxis":{"allowDecimals":false,"labels":{"formatter":{}}}}

Using JSON, you can pass key and values between 2 programming languages, but not functions. So, there is only one option is to create a related function in Javascript and use it for what you want to accomplish.

If you want to use a function at client side, see if this answer can help.



Related Topics



Leave a reply



Submit