Skip to main content

Php

Struct Php 

Source
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

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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

pub fn call(&self, callable: &Value, args: &[Value]) -> Result<Value, PhpError>

Calls a PHP callable (a function name, a [class, method] array, a closure or an invokable object) with args and returns its result.

Source§

impl Php

Source

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.

Source

pub fn class_of(&self, value: &Value) -> Option<String>

The class of a PHP object (Value::Opaque from PHP); None for anything else.

Source

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.

Source

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.

Source

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.

Source

pub fn redirect_output(&self, to: Output)

Sends PHP’s output to stdout (the default) or stderr.

Trait Implementations§

Source§

impl Drop for Php

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

§

impl !Send for Php

§

impl !Sync for Php

§

impl Forget for Php

§

impl Freeze for Php

§

impl Move for Php

§

impl RefUnwindSafe for Php

§

impl Unpin for Php

§

impl UnsafeUnpin for Php

§

impl UnwindSafe for Php

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.