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<()>
impl ClassEntity<()>
source§impl<T: Default + 'static> ClassEntity<T>
impl<T: Default + 'static> ClassEntity<T>
sourcepub fn new_with_default_state_constructor(class_name: impl Into<String>) -> Self
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>
impl<T: 'static> ClassEntity<T>
sourcepub fn new_with_state_constructor(
class_name: impl Into<String>,
state_constructor: impl Fn() -> T + 'static
) -> Self
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.
sourcepub fn add_method<F, Z, E>(
&mut self,
name: impl Into<String>,
vis: Visibility,
handler: F
) -> &mut MethodEntity
pub fn add_method<F, Z, E>( &mut self, name: impl Into<String>, vis: Visibility, handler: F ) -> &mut MethodEntity
Add member method to class, with visibility and method handler.
sourcepub fn add_static_method<F, Z, E>(
&mut self,
name: impl Into<String>,
vis: Visibility,
handler: F
) -> &mut MethodEntity
pub fn add_static_method<F, Z, E>( &mut self, name: impl Into<String>, vis: Visibility, handler: F ) -> &mut MethodEntity
Add static method to class, with visibility and method handler.
sourcepub fn add_abstract_method(
&mut self,
name: impl Into<String>,
vis: Visibility
) -> &mut MethodEntity
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).
sourcepub fn add_property(
&mut self,
name: impl Into<String>,
visibility: Visibility,
value: impl Into<Scalar>
)
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”.
sourcepub fn add_static_property(
&mut self,
name: impl Into<String>,
visibility: Visibility,
value: impl Into<Scalar>
)
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”.
sourcepub fn extends(&mut self, parent: impl Fn() -> &'static ClassEntry + 'static)
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());
sourcepub fn implements(
&mut self,
interface: impl Fn() -> &'static ClassEntry + 'static
)
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.
// ...
sourcepub fn bind(&mut self, cls: &'static StaticStateClass<T>)
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.
sourcepub fn state_cloner(&mut self, clone_fn: impl Fn(&T) -> T + 'static)
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
}