Fork Me On Github
zodream 技术 C 1周前

VS2019 开发PHP 拓展(五)创建 class

增加一个 application 类

zodream_application.h

定义类的方法

#ifndef ZODREAM_APPLICATION_H
#define ZODREAM_APPLICATION_H

#ifndef ZO_BOOTED
#define ZO_BOOTED ZEND_STRL("booted")
#endif // !ZO_BOOTED

PHP_METHOD(ZodreamApplication, __construct);
PHP_METHOD(ZodreamApplication, __destruct);
PHP_METHOD(ZodreamApplication, version);
PHP_METHOD(ZodreamApplication, handle);

extern zend_class_entry* zo_application_ce;

ZEND_MINIT_FUNCTION(zodream_appliation);

#endif //ZODREAM_APPLICATION_H

zodream_application.c

实现类的方法,并实现初始化

#include "zodream_application.h"

zend_class_entry* zo_application_ce;

/* {{{ proto ZodreamApplication ZodreamApplication::__construct()
        Public constructor */
PHP_METHOD(ZodreamApplication, __construct)
{
    // 修改属性
    zend_update_property_bool(zo_application_ce, getThis(), ZO_BOOTED, TRUE);
}
/* }}} */

/* {{{ proto void ZodreamApplication::__destruct()
*/
PHP_METHOD(ZodreamApplication, __destruct)
{

}
/* }}} */

/* {{{ proto string ZodreamApplication::version()
*/
PHP_METHOD(ZodreamApplication, version)
{
    zend_string *retval = strpprintf(0, PHP_ZODREAM_VERSION);
    RETURN_STR(retval);
}
/* }}} */

/* {{{ proto void ZodreamApplication::handle()
*/
PHP_METHOD(ZodreamApplication, handle)
{
    zend_bool* booted = (zend_bool *)zend_read_property(zo_application_ce, getThis(), ZO_BOOTED, 0, NULL);
    RETURN_BOOL(booted);
}
/* }}} */

zend_function_entry zodream_application_methods[] = {
     PHP_ME(ZodreamApplication, __destruct, arginfo_void, ZEND_ACC_PUBLIC | ZEND_ACC_DTOR )
     PHP_ME(ZodreamApplication, __construct, arginfo_void, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
     PHP_ME(ZodreamApplication, version, arginfo_void, ZEND_ACC_PUBLIC)
     PHP_ME(ZodreamApplication, handle, arginfo_void, ZEND_ACC_PUBLIC)
     PHP_FE_END
};

ZEND_MINIT_FUNCTION(zodream_application)
{
    zend_class_entry ce;
    INIT_CLASS_ENTRY(ce, "Zodream\\Application", zodream_application_methods);
    zo_application_ce = zend_register_internal_class_ex(&ce, NULL);
    zo_application_ce->ce_flags |= ZEND_ACC_FINAL;

    zend_declare_property_bool(zo_application_ce, ZO_BOOTED, FALSE, ZEND_ACC_PROTECTED);
    zend_declare_class_constant_string(zo_application_ce, ZEND_STRL("VERSION"), PHP_ZODREAM_VERSION);

    return SUCCESS;
}

zodream.c

进行类的初始化

PHP_MINIT_FUNCTION(zodream)
{
    ZEND_MODULE_STARTUP_N(zodream_application)(INIT_FUNC_ARGS_PASSTHRU)
    return SUCCESS;
}
点击查看全文
0 1 0