How can I write PHP opcode, save it in a file and make the Zend Engine execute it? Any method or hack is welcome, as long as it does the trick.
Asked
Active
Viewed 2,428 times
11
-
at "no well-intentioned answers that are not precisely addressing [the] question". Are miserable or angry answers that don't address the question acceptable? – halfer Nov 30 '11 at 13:25
-
@halfer ... look at Mo.sch's answer ... PHP-questions always attract such answers ... and did you -1 one me? oh man ... – Raffael Nov 30 '11 at 13:31
-
1No, no -1 from me; I always try to state why I downvote :) – halfer Nov 30 '11 at 13:57
-
2Why downvote? It's a perfectly valid (and interesting) question. – N.B. Nov 30 '11 at 14:01
-
1@N.B. - yes, it's a good question. The downvotes may have been for the way the question was originally asked, but it has now been nicely improved. – halfer Nov 30 '11 at 15:04
2 Answers
5
There's a couple of user-space methods (from plugins) that can deal with Opcodes.
- http://uk.php.net/apc_bin_load (and http://uk.php.net/apc_bin_dump)
- http://uk.php.net/bcompiler_read / http://uk.php.net/bcompiler_write_file
Neither produces plain text however because the opcodes are not designed to be a user-writable language (unlike Parrot).

Alister Bulman
- 34,482
- 9
- 71
- 110
-
that means I could overwrite a cached file with apc_bin_load, I guess ... so next question would be how the nice looking opcode statements relate to the actually used binary representation. do you know something about that? references or so? – Raffael Nov 30 '11 at 16:02
-
http://stackoverflow.com/questions/1795425/how-to-get-opcodes-of-php has some information, mostly referring to "Vulcan Logic Dumper" - http://pecl.php.net/package/vld – Alister Bulman Nov 30 '11 at 17:49
1
There is an extension called ulopcodes that allows you to emit your own opcodes via a function that it exposes to PHP code.
For example:
ulopcodes_emit(ZEND_ECHO, "Hello world!");
Will create that line in the current oparray which will be executed by the VM.
This extension is purely educational and not intended to be used in production code.
(Disclaimer: I am the creator of ulopcodes)

pmmaga
- 410
- 7
- 18
-
Yes, you can use [vld](https://github.com/derickr/vld) for instance to output the generated opcodes. – pmmaga Jan 16 '17 at 14:51