- 类
- 类修饰符
- 实现接口
类
每个 Zephir 文件都必须实现一个类或接口 (并且只有一个)。 类结构与 php 类非常相似:
namespace Test;/*** This is a sample class*/class MyClass{}
类修饰符
支持以下类修饰符:
final: 如果一个类有这个修饰符,它就不能被扩展:
namespace Test;/*** This class cannot be extended by another class*/final class MyClass{}
abstract: 如果一个类有这个修饰符,它不能被实例化:
namespace Test;/*** This class cannot be instantiated*/abstract class MyClass{}
实现接口
Zephir 类可以实现任意数量的接口, 前提是这些接口 显性 的引用(使用use)。 但是, 有时 Zephir 类 (以及随后的扩展) 可能需要实现在不同扩展中构建的接口。
如果我们想要实现MiddlewareInterface从PSR扩展,我们需要创建一个stub接口:
// middlewareinterfaceex.zepnamespace Test\Oo\Extend;use Psr\Http\Server\MiddlewareInterface;interface MiddlewareInterfaceEx extends MiddlewareInterface{}
从这里开始,我们可以在整个扩展过程中使用stub接口。
/*** @test*/public function shouldExtendMiddlewareInterface(){if (!extension_loaded('psr')) {$this->markTestSkipped("The psr extension is not loaded");}$this->assertTrue(is_subclass_of(MiddlewareInterfaceEx::class, 'Psr\Http\Server\MiddlewareInterface'));}
NOTE开发人员有责任确保在加载扩展之前存在所有外部引用。 因此,对于上面的示例,在加载Zephir构建的扩展之前,必须先加载PSR扩展。
