Personal tools
From wiki.swiftlytilting.com
Jump to: navigation, search

Phc-win

phc-win is an open source PHP compiler for Windows. It compiles PHP scripts to PHP-native bytecode and then produces an EXE file containing your compiled code.

Check for the latest updates to phc-win on the blog.

Contents

How phc-win works

phc-win works in two stages. The first stage is the compilation stage, and the second stage is the actual exe building stage. The PHP extension bcompiler is used to compile PHP script code into PHP bytecode. This bytecode can be included just like any php file as long as the bcompiler extension is loaded. Once all the bytecode files have been created, a modified Embeder is used to pack all of the project files into the program exe.

Compiling a single PHP file

To compile a single file, select Compile single file from the File menu. Then select the file to compile.

Compiling a project folder

To build an EXE containing all files in a directory and all sub directories, choose Compile directory from the File menu. You will be prompted to select the project folder. Once you choose the project folder, you will be asked to choose the main program file.

phc-win will then scan all the files in the directory specified and all subdirectories. All files with php anywhere in the extension will be compiled into .phb files. These .phb files, along with all other files in directory and subdirectories will be added to the project exe.

Accessing embedded files - exe_resource

IMPORTANT: When using exe_resource and relative paths, the path must be relative to the main program file, regardless of the path of the file in which exe_resource is being used.


The files stored inside of the exe can be accessed via one of the following versions of the exe_resource wrapper function. When called from an uncompiled script, it returns the file location on the drive. Add one of the following function definitions to the top of your main program code, before any file access or includes:

Simple - Relative paths only

This is the most basic version of exe_resource. Only relative paths may be used, and the path is always relative to the main program file. $file must be in the format: ./filename.php or ./directory/filename.php

function exe_resource($file)
{  return defined('EMBEDED') ? 'res:///PHP/'.strtoupper(md5($file)):(getcwd() . "/$file");
}


Complex - Most path formats accepted

This function should properly convert all windows paths to the type expected by phc-win.

function exe_resource($file) 
{  $file = str_replace('\\','/',$file);
   $notembeded = $file;
   
   if (!strpos($file, ':'))
   {  if ($file[0] != '.' && $file[0] != '/')
      {   $file = './'.$file;
      }
      $notembeded = getcwd() . ($file[0] != '/' ? '/' : '') . $file;
   } 
   
   return defined('EMBEDED') ? 'res:///PHP/'.strtoupper(md5($file)):$notembeded; 
}

Support files needed

The EXE files created with phc-win require the following support files:

  • php5ts.dll
  • php_win32std.dll
  • php_bcompiler.dll
  • php-embed.ini

All of these files are also needed to run phc-win, so you can simply copy the from the phc-win program folder. Once you have copied the support files, you will need to open php-embed.ini and comment out line which includes php_winbinder.dll.

Examples

For examples, see the source folder inside the phc-win program folder. The code there is all formatted to be compiled with phc-win.

Download

RSS Updates are available via SwiftlyTilting blog posts

phc-win 0.3.1

Compatible with PHP 5.3.1

phc-win 0.3.0

Compatible with PHP 5.3.1

phc-win 0.2.1

Compatible with PHP 5.0.5

phc-win 0.2.0

Compatible with PHP 5.0.5

See Also

Winbinder Projects