PHP扩展开发-简单类扩展

时间:2023-01-19 17:17:10

今天来学习简单类扩展开发

实现目标为如下php的类

<?php
class classext(){ private $username; CONST URL="http://www.google.com"; public function __construct(){} public function getName(){
return $this->username;
} public function setName($name){
$this->username=$name;
} public function __destruct(){} } ?>

1.先使用./ext_skel --extname=classext创建扩展框架

2.在php_classext.h中添加声明成员变量,代码如下

PHP_METHOD(classext,__construct);
PHP_METHOD(classext,__destruct);
PHP_METHOD(classext,getName);
PHP_METHOD(classext,setName);

3.在classext.c中添加相应的函数,我把所有代码贴出来大家参考

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif #include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_classext.h" static int le_classext; zend_class_entry *classext_ce; /* 类方法的参数 */
ZEND_BEGIN_ARG_INFO(setName_args, )
ZEND_ARG_INFO(, name)
ZEND_END_ARG_INFO() PHP_METHOD(classext,__construct){ } PHP_METHOD(classext,__destruct){ } PHP_METHOD(classext,getName){
zval *self,*name;
self = getThis();
name = zend_read_property(Z_OBJCE_P(self),self,ZEND_STRL("username"), TSRMLS_CC); RETURN_STRING(Z_STRVAL_P(name),);
} PHP_METHOD(classext, setName) {
char *arg = NULL;
int arg_len;
zval *value, *self; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
WRONG_PARAM_COUNT;
} self = getThis(); MAKE_STD_ZVAL(value);
ZVAL_STRINGL(value, arg, arg_len, ); SEPARATE_ZVAL_TO_MAKE_IS_REF(&value);
zend_update_property(Z_OBJCE_P(self), self, ZEND_STRL("username"), value TSRMLS_CC); RETURN_TRUE;
} const zend_function_entry classext_functions[] = {
PHP_ME(classext, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
PHP_ME(classext, __destruct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_DTOR)
PHP_ME(classext, getName, NULL, ZEND_ACC_PUBLIC)
PHP_ME(classext, setName, setName_args, ZEND_ACC_PUBLIC)
{NULL, NULL, NULL} /* Must be the last line in martin_functions[] */
}; PHP_MINIT_FUNCTION(classext)
{
zend_class_entry classext;
INIT_CLASS_ENTRY(classext, "Classext", classext_functions);
classext_ce = zend_register_internal_class_ex(&classext, NULL, NULL TSRMLS_CC);
/* 声明常量URL */
zend_declare_class_constant_stringl(classext_ce, ZEND_STRL("URL"), ZEND_STRL("http://www.google.com") TSRMLS_CC);
/* 声明私有成员变量 username
ZEND_STRL("username")等价于"username",sizeof("username")-1;详见zend.h */
zend_declare_property_null(classext_ce, ZEND_STRL("username"), ZEND_ACC_PRIVATE TSRMLS_CC); return SUCCESS;
} PHP_MSHUTDOWN_FUNCTION(classext)
{
return SUCCESS;
} PHP_RINIT_FUNCTION(classext)
{
return SUCCESS;
} PHP_RSHUTDOWN_FUNCTION(classext)
{
return SUCCESS;
} PHP_MINFO_FUNCTION(classext)
{
php_info_print_table_start();
php_info_print_table_header(, "classext support", "enabled");
php_info_print_table_header(, "The Version", "1.0.2");
php_info_print_table_end();
} zend_module_entry classext_module_entry = {
STANDARD_MODULE_HEADER,
"classext",
NULL,
PHP_MINIT(classext),
PHP_MSHUTDOWN(classext),
PHP_RINIT(classext),
PHP_RSHUTDOWN(classext),
PHP_MINFO(classext),
PHP_CLASSEXT_VERSION,
STANDARD_MODULE_PROPERTIES
}; #ifdef COMPILE_DL_CLASSEXT
ZEND_GET_MODULE(classext)
#endif

4.编译扩展;ps:记得把config.m4中的第10,12行dnl删掉

5.重启php环境,加载相应的扩展后,我们在php脚本中执行

<?php
$class = new classext();
$class->setName("xiaozong");
echo $class->getName();
?>

输出:xiaozong