1

Possible Duplicate:
reconstruct/get code of php function

In JavaScript there is simply .toString() in function object, that outputs its source.

Is there an equivalent function or, how would one write one to do the same in PHP?

I am looking to write a PHP -> JS compiler. And, since token_get_all requires source as it's parameter, I'm looking for this particular export.

Community
  • 1
  • 1
tomsseisums
  • 13,168
  • 19
  • 83
  • 145
  • Why not just decompile the AST instead? – Ignacio Vazquez-Abrams Jul 17 '12 at 08:03
  • @KarolyHorvath, PHP's `__toString()` is totally different deal. And I did a google search before, just couldn't find a thing. – tomsseisums Jul 17 '12 at 08:04
  • ahhmm.. I see.. what's a function *object* in PHP? what is that for which you want to get the source? – Karoly Horvath Jul 17 '12 at 08:10
  • @psycketom Javascript is a totally different deal to PHP (and indeed most languages) in this respect. Because of the way the PHP bytecode compiler works, there would be no way to do this at run-time. The only way I can think of (and this is not really a viable option). would be to `file_get_contents()` the file that contains the source code of the function and use some horrible regex. Or some nightmarish construct involving [Tokenizer](http://www.php.net/manual/en/function.token-get-all.php). Obviously even this would only work with user-defined functions, native functions are a none-starter. – DaveRandom Jul 17 '12 at 08:16
  • @DaveRandom, yes, I know, just thought there is an equivalent or something. Since that's a compiler, `Reflection` and/or `file_get_contents()` with Tokenizer will nail it. And, I'm not looking to export PHP's original source, only user functions, because core constructs of languages remain the same except the syntax, therefore, can be replaced from a mirror table. – tomsseisums Jul 17 '12 at 08:28

2 Answers2

0

The equivalent function to toString() in php is strval()

Usage

$set = strval($value);

PHP DOCS: String and strval

strval — Get string value of a variable

There is a descriptive answer here (regarding converting something from type to string) on stackoverflow.

Community
  • 1
  • 1
classicjonesynz
  • 4,012
  • 5
  • 38
  • 78
0

if you want the source code of a function in php you must use ReflectionFunction to find the file which it is defined in as well as start and end line.

ReflectionFunction::getFileName();
ReflectionFunction::getStartLine();
ReflectionFunction::getEndLine();

Then you must read the file and substr that portion.

Ali
  • 21,572
  • 15
  • 83
  • 95