搜尋
首頁後端開發php教程用C兑现PHP扩展类

用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!







陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
高流量網站的PHP性能調整高流量網站的PHP性能調整May 14, 2025 am 12:13 AM

TheSecretTokeEpingAphp-PowerEdwebSiterUnningSmoothlyShyunderHeavyLoadInVolvOLVOLVOLDEVERSALKEYSTRATICES:1)emplactopCodeCachingWithOpcachingWithOpCacheToreCescriptexecution Time,2)使用atabasequercachingCachingCachingWithRedataBasEndataBaseLeSendataBaseLoad,3)

PHP中的依賴注入:初學者的代碼示例PHP中的依賴注入:初學者的代碼示例May 14, 2025 am 12:08 AM

你應該關心DependencyInjection(DI),因為它能讓你的代碼更清晰、更易維護。 1)DI通過解耦類,使其更模塊化,2)提高了測試的便捷性和代碼的靈活性,3)使用DI容器可以管理複雜的依賴關係,但要注意性能影響和循環依賴問題,4)最佳實踐是依賴於抽象接口,實現鬆散耦合。

PHP性能:是否可以優化應用程序?PHP性能:是否可以優化應用程序?May 14, 2025 am 12:04 AM

是的,優化papplicationispossibleandessential.1)empartcachingingcachingusedapcutorediucedsatabaseload.2)優化的atabaseswithexing,高效Quereteries,and ConconnectionPooling.3)EnhanceCodeWithBuilt-unctions,避免使用,避免使用ingglobalalairaiables,並避免使用

PHP性能優化:最終指南PHP性能優化:最終指南May 14, 2025 am 12:02 AM

theKeyStrategiestosigantificallyBoostPhpaPplicationPerformenCeare:1)UseOpCodeCachingLikeLikeLikeLikeLikeCacheToreDuceExecutiontime,2)優化AtabaseInteractionswithPreparedStateTementStatementStatementAndProperIndexing,3)配置

PHP依賴注入容器:快速啟動PHP依賴注入容器:快速啟動May 13, 2025 am 12:11 AM

aphpdepentioncontiveContainerIsatoolThatManagesClassDeptions,增強codemodocultion,可驗證性和Maintainability.itactsasaceCentralHubForeatingingIndections,因此reducingTightCightTightCoupOulplingIndeSingantInting。

PHP中的依賴注入與服務定位器PHP中的依賴注入與服務定位器May 13, 2025 am 12:10 AM

選擇DependencyInjection(DI)用於大型應用,ServiceLocator適合小型項目或原型。 1)DI通過構造函數注入依賴,提高代碼的測試性和模塊化。 2)ServiceLocator通過中心註冊獲取服務,方便但可能導致代碼耦合度增加。

PHP性能優化策略。PHP性能優化策略。May 13, 2025 am 12:06 AM

phpapplicationscanbeoptimizedForsPeedAndeffificeby:1)啟用cacheInphp.ini,2)使用preparedStatatementSwithPdoforDatabasequesies,3)3)替換loopswitharray_filtaray_filteraray_maparray_mapfordataprocrocessing,4)conformentnginxasaseproxy,5)

PHP電子郵件驗證:確保正確發送電子郵件PHP電子郵件驗證:確保正確發送電子郵件May 13, 2025 am 12:06 AM

phpemailvalidation invoLvesthreesteps:1)格式化進行regulareXpressecthemailFormat; 2)dnsvalidationtoshethedomainhasavalidmxrecord; 3)

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境