用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 <br><p>4,编译代码</p><p></p><pre name="code" class="php">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!

在html5中,width的意思是宽度,width属性定义元素内容区的宽度,在内容区外面可以增加内边距、边框和外边距,只需要给元素设置“元素{width:数值}”即可。

如何在Zend框架中使用ACL(AccessControlList)进行权限控制导言:在一个Web应用程序中,权限控制是至关重要的一项功能。它可以确保用户只能访问其有权访问的页面和功能,并防止未经授权的访问。Zend框架提供了一种方便的方法来实现权限控制,即使用ACL(AccessControlList)组件。本文将介绍如何在Zend框架中使用ACL

PHP实现框架:ZendFramework入门教程ZendFramework是PHP开发的一种开源网站框架,目前由ZendTechnologies维护,ZendFramework采用了MVC设计模式,提供了一系列可重用的代码库,服务于实现Web2.0应用程序和Web服务。ZendFramework深受PHP开发者的欢迎和推崇,拥有广泛

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code<form name="myform"

CSS维度属性详解:height和width在前端开发中,CSS是一种强大的样式定义语言。其中,height和width是两个最基本的维度属性,用于定义元素的高度和宽度。本文将对这两个属性进行详细解析,并提供具体的代码示例。一、height属性height属性用于定义元素的高度。可以使用像素(pixel)、百分比(percentage)或者

PHP无法识别ZendOptimizer,如何解决?在PHP开发中,有时可能会遇到PHP无法识别ZendOptimizer的情况,这会导致部分PHP代码无法正常运行。在这种情况下,我们需要采取一些措施来解决这个问题。下面将介绍一些可能的解决方法,并附上具体的代码示例。1.确认ZendOptimizer是否正确安装:首先,我们需要确认ZendOptimize

Windows2003安装包中包含了Zend,PHP5.2.17,PHPWind8.7和PHPMyadmin3.5.2,您可以直接下载安装包,节约搜索资源的时间。 但是,由于MySQL超出了上传限制,您需要另行前往MySQL官网下载。然后解压拷贝到D盘,如下图: MySQLinDdisk 安装与配置WindowsIIS+FTP 单击开始>控制面板>添加或删除程序。 AddingordeletingaPG 单击添加/删除Windows组件(A)。 Addingorde

方法有像素值、百分比、em单位、rem单位、vw/vh单位、auto、fit-content、min-content、max-content。详细介绍:1、像素值(px):像素值是固定的,不论屏幕分辨率如何变化,它的宽度都是不变的。例如:width: 300px;2、百分比(%):百分比宽度是相对于父元素的宽度的。例如:width: 50%;3、em单位等等。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version
Recommended: Win version, supports code prompts!

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
