publicloadPHPRuntime
Callable
Parameters
phpLoaderModule: PHPLoaderModule
The ESM-wrapped Emscripten module. Consult the Dockerfile for the build process.
phpModuleArgs: EmscriptenOptions = {}
The Emscripten module arguments, see https://emscripten.org/docs/api_reference/module.html#affecting-execution.
Returns Promise<number>
Loaded runtime id.
Loads the PHP runtime with the given arguments and data dependencies.
This function handles the entire PHP initialization pipeline. In particular, it:
Basic usage:
The PHP loader module:
In the basic usage example,
phpLoaderModule
is not a vanilla Emscripten module. Instead, it’s an ESM module that wraps the regular Emscripten output and adds some extra functionality. It’s generated by the Dockerfile shipped with this repo. Here’s the API it provides:PHP Filesystem:
Once initialized, the PHP has its own filesystem separate from the project files. It’s provided by Emscripten and uses its FS library.
The API exposed to you via the PHP class is succinct and abstracts certain unintuitive parts of low-level filesystem interactions.
Here’s how to use it:
For more details consult the PHP class directly.
Data dependencies:
Using existing PHP packages by manually recreating them file-by-file would be quite inconvenient. Fortunately, Emscripten provides a “data dependencies” feature.
Data dependencies consist of a
dependency.data
file and adependency.js
loader and can be packaged with the file_packager.py tool. This project requires wrapping the Emscripten-generateddependency.js
file in an ES module as follows:export default function(emscriptenPHPModule) {';
export const dependencyFilename = '<DATA FILE NAME>';
export const dependenciesTotalSize = <DATA FILE SIZE>;
}
Be sure to use the
--export-name="emscriptenPHPModule"
file_packager.py option.You want the final output to look as follows:
Such a constructions enables loading the
dependency.js
as an ES Module usingimport("/dependency.js")
.Once it’s ready, you can load PHP and your data dependencies as follows: