PHP
Implements
- Disposable
Index
Properties
Accessors
Methods
- [dispose]
- addEventListener
- chdir
- cli
- defineConstant
- dispatchEvent
- exit
- fileExists
- hotSwapPHPRuntime
- initializeRuntime
- internalUrlToPath
- isDir
- isFile
- isSymlink
- listFiles
- mkdir
- mkdirTree
- mount
- mv
- onMessage
- pathToInternalUrl
- readFileAsBuffer
- readFileAsText
- readlink
- realpath
- removeEventListener
- request
- rmdir
- run
- setSapiName
- setSkipShebang
- setSpawnHandler
- symlink
- unlink
- writeFile
Properties
optionalrequestHandler
semaphore
An exclusive lock that prevent multiple requests from running at the same time.
Accessors
absoluteUrl
Returns string
documentRoot
Returns string
Methods
[dispose]
Returns void
addEventListener
Adds an event listener for a PHP event.
Parameters
eventType: request.end | request.error | runtime.initialized | runtime.beforedestroy
The type of event to listen for.
listener: PHPEventListener
The listener function to be called when the event is triggered.
Returns void
chdir
Changes the current working directory in the PHP filesystem. This is the directory that will be used as the base for relative paths. For example, if the current working directory is
/root/php
, and the path isdata
, the absolute path will be/root/php/data
.Parameters
path: string
The new working directory.
Returns void
cli
Starts a PHP CLI session with given arguments.
This method can only be used when PHP was compiled with the CLI SAPI and it cannot be used in conjunction with
run()
.Once this method finishes running, the PHP instance is no longer usable and should be discarded. This is because PHP internally cleans up all the resources and calls exit().
Parameters
argv: string[]
The arguments to pass to the CLI.
Returns Promise<number>
The exit code of the CLI session.
defineConstant
Defines a constant in the PHP runtime.
Parameters
key: string
The name of the constant.
value: null | string | number | boolean
The value of the constant.
Returns void
dispatchEvent
Type parameters
- Event: PHPEvent
Parameters
event: Event
Returns void
exit
Parameters
code: number = 0
Returns void
fileExists
Checks if a file (or a directory) exists in the PHP filesystem.
Parameters
path: string
The file path to check.
Returns boolean
True if the file exists, false otherwise.
hotSwapPHPRuntime
Hot-swaps the PHP runtime for a new one without interrupting the operations of this PHP instance.
Parameters
runtime: number
optionalcwd: string
Returns void
initializeRuntime
Parameters
runtimeId: number
Returns void
internalUrlToPath
Parameters
internalUrl: string
Returns string
isDir
Checks if a directory exists in the PHP filesystem.
Parameters
path: string
– The path to check.
Returns boolean
True if the path is a directory, false otherwise.
isFile
Checks if a file exists in the PHP filesystem.
Parameters
path: string
– The path to check.
Returns boolean
True if the path is a file, false otherwise.
isSymlink
Checks if a path is a symlink in the PHP filesystem.
Parameters
path: string
Returns boolean
True if the path is a symlink, false otherwise.
listFiles
Lists the files and directories in the given directory.
Parameters
path: string
The directory path to list.
options: ListFilesOptions = ...
Options for the listing.
Returns string[]
The list of files and directories in the given directory.
mkdir
Recursively creates a directory with the given path in the PHP filesystem. For example, if the path is
/root/php/data
, and/root
already exists, it will create the directories/root/php
and/root/php/data
.Parameters
path: string
The directory path to create.
Returns void
mkdirTree
Parameters
path: string
Returns void
mount
Mounts a filesystem to a given path in the PHP filesystem.
Parameters
virtualFSPath: string
Where to mount it in the PHP virtual filesystem.
mountHandler: MountHandler
The mount handler to use.
Returns Promise<UnmountFunction>
Unmount function to unmount the filesystem.
mv
Moves a file or directory in the PHP filesystem to a new location.
Parameters
fromPath: string
toPath: string
Returns void
onMessage
Listens to message sent by the PHP code.
To dispatch messages, call:
post_message_to_js(string $data)
Arguments:
$data (string) – Data to pass to JavaScript.Parameters
listener: MessageListener
Callback function to handle the message.
Returns void
pathToInternalUrl
Parameters
path: string
Returns string
readFileAsBuffer
Reads a file from the PHP filesystem and returns it as an array buffer.
Parameters
path: string
The file path to read.
Returns Uint8Array
The file contents.
readFileAsText
Reads a file from the PHP filesystem and returns it as a string.
Parameters
path: string
The file path to read.
Returns string
The file contents.
readlink
Reads the target of a symlink in the PHP filesystem.
Parameters
path: string
Returns string
The target of the symlink.
realpath
Resolves the real path of a file in the PHP filesystem.
Parameters
path: string
Returns string
The real path of the file.
removeEventListener
Removes an event listener for a PHP event.
Parameters
eventType: request.end | request.error | runtime.initialized | runtime.beforedestroy
The type of event to remove the listener from.
listener: PHPEventListener
The listener function to be removed.
Returns void
request
Do not use. Use new PHPRequestHandler() instead.
Parameters
request: PHPRequest
Returns Promise<PHPResponse>
rmdir
Removes a directory from the PHP filesystem.
Parameters
path: string
The directory path to remove.
options: RmDirOptions = ...
Options for the removal.
Returns void
run
Runs PHP code.
This low-level method directly interacts with the WebAssembly PHP interpreter.
Every time you call run(), it prepares the PHP environment and:
- Resets the internal PHP state
- Populates superglobals ($_SERVER, $_GET, etc.)
- Handles file uploads
- Populates input streams (stdin, argv, etc.)
- Sets the current working directory
You can use run() in two primary modes:
Code snippet mode
In this mode, you pass a string containing PHP code to run.
const result = await php.run({
code: `<?php echo "Hello world!";`
});
// result.text === "Hello world!"In this mode, information like DIR or FILE isn’t very useful because the code is not associated with any file.
Under the hood, the PHP snippet is passed to the
zend_eval_string
C function.File mode
In the file mode, you pass a scriptPath and PHP executes a file found at a that path:
php.writeFile(
"/www/index.php",
`<?php echo "Hello world!";"`
);
const result = await php.run({
scriptPath: "/www/index.php"
});
// result.text === "Hello world!"In this mode, you can rely on path-related information like DIR or FILE.
Under the hood, the PHP file is executed with the
php_execute_script
C function.The
run()
method cannot be used in conjunction withcli()
.Parameters
request: PHPRunOptions
Returns Promise<PHPResponse>
setSapiName
Parameters
newName: string
Returns Promise<void>
setSkipShebang
Parameters
shouldSkip: boolean
Returns void
setSpawnHandler
Parameters
handler: string | SpawnHandler
Returns Promise<void>
symlink
Creates a symlink in the PHP filesystem.
Parameters
target: string
path: string
Returns any
unlink
Removes a file from the PHP filesystem.
Parameters
path: string
The file path to remove.
Returns void
writeFile
Overwrites data in a file in the PHP filesystem. Creates a new file if one doesn’t exist yet.
Parameters
path: string
The file path to write to.
data: string | Uint8Array
The data to write to the file.
Returns void
An environment-agnostic wrapper around the Emscripten PHP runtime that universals the super low-level API and provides a more convenient higher-level API.
It exposes a minimal set of methods to run PHP scripts and to interact with the PHP filesystem.