您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

用C兑现PHP扩展类

2024/2/25 4:16:55发布21次查看
用c实现php扩展类
前面简单介绍了用c语言实现php扩展的步骤,见用c开发php扩展的步骤,那个是扩展一个函数,这里讲述一下如何用c扩展类。
    准备实现的类如下:
class rectangle{ private $_width; private $_height; public function __construct($width, $height){ $this->_width = $width; $this->_height = $height; } public function clone(){ return new rectangle($this->_width, $this->_height); } public function setwidth($width){ $this->_width = $width; } public function setheight($height){ $this->_height = $height; } public function getwidth(){ return $this->_width; } public function getheight(){ return $this->_height; } public function getarea(){ return $this->_width * $this->_height; } public function getcircle(){ return ($this->_width + $this->_height) * 2; }}
实现类扩展的步骤如下:(首先下载php源码,这里使用的是php-5.2.8)
1,建立扩展骨架
cd php-5.2.8/ext./ext_skel --extname=class_ext
2,修改编译参数
cd php-5.2.8/ext/class_extvi config.m4
去掉php_arg_enable(class_ext, whether to enable class_ext support,和
[   --enable-class_ext       enable class_ext support])两行前面的dnl,修改后为:
dnl otherwise use enable: php_arg_enable(class_ext, whether to enable class_ext support, dnl make sure that the comment is aligned: [ --enable-class_ext enable class_ext support])
3,编写c代码
cd php-5.2.8/ext/class_extvi php_class_ext.h#在 php_function(confirm_class_ext_compiled); 后面增加申明函数;
php_method(rectangle,__construct);php_method(rectangle,clone);php_method(rectangle,setwidth);php_method(rectangle,setheight);php_method(rectangle,getwidth);php_method(rectangle,getheight);php_method(rectangle,getarea);php_methdo(rectangle,getcircle);
vi class_ext.c#申明方法的参数,注册到函数表中
zend_begin_arg_info(arg_construct,2)zend_arg_info(0, width)zend_arg_info(0, height)zend_end_arg_info()zend_begin_arg_info(arg_set_width,1)zend_arg_info(0, width)zend_end_arg_info()zend_begin_arg_info(arg_set_height,1)zend_arg_info(0, height)zend_end_arg_info()const zend_function_entry class_ext_functions[] = { php_fe(confirm_class_ext_compiled, null) php_me(rectangle, __construct, arg_construct, zend_acc_ctor|zend_acc_public) php_me(rectangle, clone, null, zend_acc_public) php_me(rectangle, setwidth, null, zend_acc_public) php_me(rectangle, setheight, null, zend_acc_public) php_me(rectangle, getwidth, null, zend_acc_public) php_me(rectangle, getheight, null, zend_acc_public) php_me(rectangle, getarea, null, zend_acc_public) php_me(rectangle, getcircle, null, zend_acc_public) {null, null, null} /* must be the last line in class_ext_functions[] */};
#其中zend_acc_ctor表示构造函数,zend_acc_public表示访问权限为public。
#接下来,在模块初始化函数中注册并初始化类
zend_class_entry *rectangle_ce; //zend内部类结构变量php_minit_function(class_ext){ zend_class_entry rectangle; init_class_entry(rectanble, rectangle, class_ext_functions); //第二个参数为类名,第三个参数为类的函数列表 rectangle_ce = zend_register_internal_class_ex(&rectangle, null, null tsrmls_cc); //注册类 zend_declare_property_null(rectangle_ce, zend_strl(_width), zend_acc_private tsrmls_cc); //初始化类的属性_width zend_declare_property_null(rectangle_ce, zend_strl(_height), zend_acc_private tsrmls_cc); //初始化类的属性_height return success;}
#在文件最后增加类的成员函数的具体实现代码
php_method(rectangle, __construct){ long width,height; if(zend_parse_parameters(zend_num_args() tsrmls_cc, ll, &width, &height) == failure){ //获取构造函数的两个函数参数_width和_height wrong_param_count; } if( width
4,编译代码
cd php-5.2.8/ext/class_ext/usr/local/php/bin/phpize./configure --with-php-config=/usr/local/php/bin/php-configmake make install
此时会在php的安装路径下产生一个so文件,比如
/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/class_ext.so
修改php.ini 添加扩展extension_dir = /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/
[class_ext]
extension = class_ext.so
5,测试代码
$width = -10;$height = 12;$rectangle = new rectangle($width, $height);$area = $rectangle->getarea();var_dump($area);$circle = $rectangle->getcircle();var_dump($circle);$clone = $rectangle->clone();$_area = $clone->getarea();var_dump($_area);$clone->setwidth(100);$clone->setheight(200);$_area = $clone->getarea();var_dump($_area);$width = $clone->getwidth();var_dump($width);$height = $clone->getheight();var_dump($height);
结果输出:
int(12)int(26)int(12)int(20000)int(100)int(200)
6,over!
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product