pub struct Php { /* private fields */ }Expand description
A booted PHP engine with one request open.
There is at most one per process, and it is neither Send nor Sync:
the build is non-thread-safe, so PHP must only be touched from the thread
that booted it. Dropping it shuts PHP down.
§PHP values that outlive the engine
Dropping Php first releases every PHP value Rust has already let go of,
then ends the request, which destroys every PHP object, including those
Rust still holds (PHP callbacks in the registry, Value::Opaques kept by
Rust code). Those handles are inert from then on: dropping one releases
nothing (the engine freed the memory), and firing a hook whose callback is
a PHP callable returns a CallbackError without touching PHP.
Implementations§
Source§impl Php
impl Php
Sourcepub fn boot(options: BootOptions<'_>) -> Result<Self, PhpError>
pub fn boot(options: BootOptions<'_>) -> Result<Self, PhpError>
Starts PHP, opens the request that all later calls run in, defines
SpeedySpec\Hooks\HookException and the CLI’s STDIN, STDOUT and
STDERR constants, and evaluates options.preload.
php.ini and the INI scan directory are never read. Like the PHP CLI,
the engine skips a leading #! line in every file it compiles.
Sourcepub fn restart_request(&self) -> Result<(), PhpError>
pub fn restart_request(&self) -> Result<(), PhpError>
Ends the current request and starts a fresh one, as a php-fpm worker does between requests: every PHP value
of the old request is freed (each area’s request_end runs), and the new one runs the request setup again.
Sourcepub fn serve(&self, request: &mut dyn WebRequest, script: &Path) -> Served
pub fn serve(&self, request: &mut dyn WebRequest, script: &Path) -> Served
Serves one web request: the open request (if any) ends, request becomes the request PHP sees (its
parameters as $_SERVER, its body as php://input, PHP’s headers and output sent to it), the request setup
runs (each area’s request_start), script runs, and the request ends (each area’s request_end). No
request is open afterwards. An exception or fatal error before any output makes the response a 500.
Sourcepub fn eval(&self, label: &str, code: &str) -> Result<Value, PhpError>
pub fn eval(&self, label: &str, code: &str) -> Result<Value, PhpError>
Runs PHP statements (no <?php tag) and returns what a top-level
return gives back, or null. label names the code in PHP’s error
messages, where a file name would otherwise go.
Sourcepub fn require(&self, path: &Path) -> Result<Value, PhpError>
pub fn require(&self, path: &Path) -> Result<Value, PhpError>
requires a PHP file and returns its return value (1 if it returns
nothing). A relative path is resolved against the working directory,
never against include_path. Exceptions, fatal errors and exit()
come back as errors.
Sourcepub fn run_file(&self, path: &Path, argv: &[String]) -> Result<i32, PhpError>
pub fn run_file(&self, path: &Path, argv: &[String]) -> Result<i32, PhpError>
Runs a PHP file the way php <path> <argv...> would and returns its
exit status: 0 when it ends normally, n when it calls exit(n).
$_SERVER['SCRIPT_NAME'], SCRIPT_FILENAME and PHP_SELF are the path.
$argv (and $_SERVER['argv']) is [path, ...argv] and $argc its
length. An uncaught exception or fatal error is an Err.
Source§impl Php
impl Php
Sourcepub fn construct(&self, class: &str, args: &[Value]) -> Result<Value, PhpError>
pub fn construct(&self, class: &str, args: &[Value]) -> Result<Value, PhpError>
Creates a PHP object: new $class(...$args). An unknown class or a
constructor that throws is the exception PHP raised.
Sourcepub fn class_of(&self, value: &Value) -> Option<String>
pub fn class_of(&self, value: &Value) -> Option<String>
The class of a PHP object (Value::Opaque from PHP); None for
anything else.
Sourcepub fn properties_of(&self, value: &Value) -> Option<Vec<(String, Value)>>
pub fn properties_of(&self, value: &Value) -> Option<Vec<(String, Value)>>
The public properties of a PHP object, in declaration then insertion
order; None for anything that is not a PHP object, or if one of the
properties cannot be converted.
Sourcepub fn poisoned(&self) -> bool
pub fn poisoned(&self) -> bool
True once PHP has bailed out (a fatal error, memory exhaustion, or a Rust panic carried through PHP) at least once; never cleared.
The engine keeps working after a bailout, but degraded for the rest of
its single request: PHP turns the cycle collector off (gc_protect),
so reference cycles are no longer freed; a fatal error during
compilation leaves the file it was compiling open and its compiler
state behind; a fatal error part-way through an engine operation can
leave engine structures half-updated; and destructors of every object
that existed at the time of a fatal error never run. exit() does not
poison: in PHP 8 it unwinds as an exception, not a bailout. Memory
exhaustion does: PHP unwinds with the same object as exit(), but
reports a fatal error (a bailout of its own) on the way, and it is
returned as PhpError::Fatal. The caller decides whether to keep
going.
Sourcepub fn set_owner(&self, owner: Option<&str>)
pub fn set_owner(&self, owner: Option<&str>)
Names the plugin that later Hook::add calls are registered for, until
changed. Hook errors from a failing callback name its owner.
Sourcepub fn redirect_output(&self, to: Output)
pub fn redirect_output(&self, to: Output)
Sends PHP’s output to stdout (the default) or stderr.