The nature of scripting language is to expose the source code that make it easy to run cross platform. However, in commercial project, you may want to protect your precious software libraries or the whole project from exposing the source code. You can do it with "bcompiler", a PECL module, that will compile PHP source code into binary bytecodes.
It is easy to install using PECL package management software as follows.
pecl install bcompiler-0.9.3
After installation, it is necessary to add a line into php.ini to make it dynamically load bcompiler module on demand.
extension=bcompiler.so
Let us start with a simple program written to run as a standalone program.
File: lib.inc -- define constants named VERSION and NEWLINE
<? // lib.inc
define("VERSION", "0.1");
define("NEWLINE", "\n");
?>
File: test.php -- display with constants defined in lib.inc
<? // test.php
require("lib.inc");
echo "Version: ".VERSION.NEWLINE;
echo "Hello World !".NEWLINE;
?>
With "php test.php" command, you should see these on screen.
Version: 0.1
Hello World !
That mean our script is working properly.
To compile the source code, we need to write another script that read a source file and write out a binary bytecodes file.
File: compile.php -- compile the code, write out binary bytecode
<? // compile.php
$f = fopen('test-o.php', 'w');
bcompiler_write_header($f);
bcompiler_write_file($f, 'test.php');
bcompiler_write_footer($f);
fclose($f);
?>
Running "php compile.php" will generate "test-o.php" file as a result. This "test-o.php" will work like original "test.php". However file size of the bytecodes seem to be much bigger than the original source code but it can improve performance by about 30% (as stated in the manual).
Not only the main ".php" code can be compiled but also the "lib.inc" library code.











