Struct phper::classes::ClassEntity

source ·
pub struct ClassEntity<T: 'static> { /* private fields */ }
Expand description

Builder for registering class.

<T> means the type of holding state.

It is a common practice for PHP extensions to use PHP objects to package third-party resources.

Implementations§

source§

impl ClassEntity<()>

source

pub fn new(class_name: impl Into<String>) -> Self

Construct a new ClassEntity with class name, do not own state.

source§

impl<T: Default + 'static> ClassEntity<T>

source

pub fn new_with_default_state_constructor(class_name: impl Into<String>) -> Self

Construct a new ClassEntity with class name and default state constructor.

source§

impl<T: 'static> ClassEntity<T>

source

pub fn new_with_state_constructor( class_name: impl Into<String>, state_constructor: impl Fn() -> T + 'static ) -> Self

Construct a new ClassEntity with class name and the constructor to build state.

source

pub fn add_method<F, Z, E>( &mut self, name: impl Into<String>, vis: Visibility, handler: F ) -> &mut MethodEntity
where F: Fn(&mut StateObj<T>, &mut [ZVal]) -> Result<Z, E> + 'static, Z: Into<ZVal> + 'static, E: Throwable + 'static,

Add member method to class, with visibility and method handler.

source

pub fn add_static_method<F, Z, E>( &mut self, name: impl Into<String>, vis: Visibility, handler: F ) -> &mut MethodEntity
where F: Fn(&mut [ZVal]) -> Result<Z, E> + 'static, Z: Into<ZVal> + 'static, E: Throwable + 'static,

Add static method to class, with visibility and method handler.

source

pub fn add_abstract_method( &mut self, name: impl Into<String>, vis: Visibility ) -> &mut MethodEntity

Add abstract method to class, with visibility (shouldn’t be private).

source

pub fn add_property( &mut self, name: impl Into<String>, visibility: Visibility, value: impl Into<Scalar> )

Declare property.

The argument value should be Copy because ‘zend_declare_property’ receive only scalar zval , otherwise will report fatal error: “Internal zvals cannot be refcounted”.

source

pub fn add_static_property( &mut self, name: impl Into<String>, visibility: Visibility, value: impl Into<Scalar> )

Declare static property.

The argument value should be Copy because ‘zend_declare_property’ receive only scalar zval , otherwise will report fatal error: “Internal zvals cannot be refcounted”.

source

pub fn extends(&mut self, parent: impl Fn() -> &'static ClassEntry + 'static)

Register class to extends the parent class.

Because in the MINIT phase, the class starts to register, so the closure is used to return the ClassEntry to delay the acquisition of the class.

Examples
use phper::classes::{ClassEntity, ClassEntry};

let mut class = ClassEntity::new("MyException");
class.extends(|| ClassEntry::from_globals("Exception").unwrap());
source

pub fn implements( &mut self, interface: impl Fn() -> &'static ClassEntry + 'static )

Register class to implements the interface, due to the class can implement multi interface, so this method can be called multi time.

Because in the MINIT phase, the class starts to register, so the closure is used to return the ClassEntry to delay the acquisition of the class.

Examples
use phper::classes::{ClassEntity, ClassEntry};

let mut class = ClassEntity::new("MyClass");
class.implements(|| ClassEntry::from_globals("Stringable").unwrap());

// Here you have to the implement the method `__toString()` in `Stringable`
// for `MyClass`, otherwise `MyClass` will become abstract class.
// ...
source

pub fn bind(&mut self, cls: &'static StaticStateClass<T>)

Bind to static StaticStateClass.

When the class registered, the StaticStateClass will be initialized, so you can use the StaticStateClass to new stateful object, etc.

source

pub fn state_cloner(&mut self, clone_fn: impl Fn(&T) -> T + 'static)

Add the state clone function, called when cloning PHP object.

By default, the object registered by phper is uncloneable, if you clone the object in PHP like this:

$foo = new Foo();
$foo2 = clone $foo;

Will throw the Error: Uncaught Error: Trying to clone an uncloneable object of class Foo.

And then, if you want the object to be cloneable, you should register the state clone method for the class.

Examples
use phper::classes::ClassEntity;

fn make_foo_class() -> ClassEntity<i64> {
    let mut class = ClassEntity::new_with_state_constructor("Foo", || 123456);
    class.state_cloner(Clone::clone);
    class
}

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for ClassEntity<T>

§

impl<T> !Send for ClassEntity<T>

§

impl<T> !Sync for ClassEntity<T>

§

impl<T> Unpin for ClassEntity<T>
where T: Unpin,

§

impl<T> !UnwindSafe for ClassEntity<T>

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>,

§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

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

§

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.