Skip to content

allow static interface methods #198

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion phper-doc/doc/_06_module/_07_register_interface/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ interface Foo extends ArrayAccess, Iterator {}

## Add methods

Interface can add public abstract methods.
Interface can add public abstract methods, and public static abstract methods.

```rust,no_run
use phper::classes::{InterfaceEntity, ClassEntry, Visibility};
Expand All @@ -68,6 +68,7 @@ use phper::values::ZVal;

let mut foo = InterfaceEntity::new("Foo");
foo.add_method("doSomethings").argument(Argument::new("name"));
foo.add_static_method("test");
```

Note that abstract has no method body, so you don't need to add the handler to the method.
Expand Down
9 changes: 9 additions & 0 deletions phper/src/classes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,15 @@ impl InterfaceEntity {
self.method_entities.last_mut().unwrap()
}

/// Add static member method to interface.
pub fn add_static_method(&mut self, name: impl Into<String>) -> &mut MethodEntity {
let mut entity = MethodEntity::new(name, None, Visibility::Public);
entity.set_vis_abstract();
entity.set_vis_static();
self.method_entities.push(entity);
self.method_entities.last_mut().unwrap()
}

/// Add constant to interface
pub fn add_constant(&mut self, name: impl Into<String>, value: impl Into<Scalar>) {
let constant = ConstantEntity::new(name, value);
Expand Down
2 changes: 2 additions & 0 deletions tests/integration/src/classes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ fn integrate_i_bar(module: &mut Module) {
.add_method("doSomethings")
.argument(Argument::new("job_name"));

interface.add_static_method("myStaticMethod");

module.add_interface(interface);
}

Expand Down
6 changes: 6 additions & 0 deletions tests/integration/tests/php/classes.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@
$doSomethings = $interface->getMethod("doSomethings");
assert_true($doSomethings->isPublic());
assert_true($doSomethings->isAbstract());
assert_false($doSomethings->isStatic());

$myStaticMethod = $interface->getMethod("myStaticMethod");
assert_true($myStaticMethod->isPublic());
assert_true($myStaticMethod->isAbstract());
assert_true($myStaticMethod->isStatic());

// Test get or set static properties.
assert_eq(IntegrationTest\PropsHolder::$foo, "bar");
Expand Down
Loading