codecamp

PHP 定义一个接口

定义一个接口

定义一个接口还是很方便的,我先给出一个PHP语言中的形式。

<?php
interface i_myinterface
{
    public function hello();
}

那它在扩展中的实现是这样的。

zend_class_entry *i_myinterface_ce;

static zend_function_entry i_myinterface_method[]={
    ZEND_ABSTRACT_ME(i_myinterface, hello, NULL) //注意这里的null指的是arginfo
    {NULL,NULL,NULL}
};

ZEND_MINIT_FUNCTION(test)
{   
    zend_class_entry ce;
    INIT_CLASS_ENTRY(ce, "i_myinterface", i_myinterface_method);

    i_myinterface_ce = zend_register_internal_interface(&ce TSRMLS_CC);
    return SUCCESS;
}

我们使用ZEND_ABSTRACT_ME()宏函数来为这个接口添加函数,它的作用是声明一个类似虚函数的东西,不用实现。也就是说我们不用为其添加ZEND_METHOD(i_myinterface,hello){...}的实现。但是这个宏函数只能为我们实现public类型函数的声明,如果有其它特殊需要,需要使用ZEND_FENTRY()宏函数来实现,因为ZEND_ABSTRACT_ME只不过是后者的一种封装。

下面我们在PHP语言中使用这个接口

<?php
class sample implements i_myinterface
{
    public $name = "hello world!";

    public function hello()
    {
        echo $this->name."\n";
    }
}

$obj = new sample();
$obj->hello();
PHP 定义一个类
PHP 类的继承与接口的实现
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

PHP ini配置文件

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }