小編給大家分享一下php7擴展類怎么實現,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
在php7中,有許許多多的擴展類,今天我們就以person類為例實現doing方法和saying方法,有需要的小伙伴可以參考一下。
1.需要實現的細節
實現一個person類
實現一個doing方法和saying方法
2.第一個擴展
2.1創建類的擴展:
[root@bogon ext]# cd /usr/local/src/php-7.0.3/ext [root@bogon ext]# ./ext_skel --extname=person
2.2 修改配置
[root@bogon ext]# vim person/config.m4 dnl PHPARGWITH(person, for person support, dnl Make sure that the comment is aligned: dnl [ --with-person Include person support])
更改為:
PHPARGWITH(person, for person support, dnl Make sure that the comment is aligned: [ --with-person Include person support])
2.3 實現代碼
在php_person.h頭中加上
extern zend_class_entry *person_ce; PHP_METHOD(person_ce,__construct); PHP_METHOD(person_ce,saying); PHP_METHOD(person_ce,doing);
在person.c頭中加上
/** * 聲明構造函數 *
@param * @return */
ZEND_METHOD(person,__construct){
zval *pThis;
pThis = getThis();
zend_printf("construct\n");
}
/** * 聲明析造函數 *
@param * @return */
ZEND_METHOD(person,__destruct){
zend_printf("destruct\n");
}
ZEND_METHOD(person,doing){
zend_printf("doing\n");
}
ZEND_METHOD(person,saying){
zend_printf("saying\n");
}
//這個函數需要加上聲明,去掉了沒用的test函數const zend_function_entry person_functions[] = {
ZEND_ME(person, __construct, global_config_arg, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
ZEND_ME(person,doing,NULL,ZEND_ACC_PUBLIC) ZEND_ME(person,saying,NULL,ZEND_ACC_PUBLIC)
ZEND_ME(person,__destruct,NULL,ZEND_ACC_PUBLIC|ZEND_ACC_DTOR)
PHP_FE_END
/* Must be the last line in person_functions[] */
};
//將類和方法注冊到zendPHP_MINIT_FUNCTION(person){
zend_class_entry ce;
INIT_CLASS_ENTRY(ce, "person", person_functions);
person_ce = zend_register_internal_class(&ce TSRMLS_CC);
zend_declare_property_null(person_ce,"saying",strlen("saying"),ZEND_ACC_PUBLIC);
zend_declare_property_null(person_ce,"doing",strlen("doing"),ZEND_ACC_PUBLIC);
return SUCCESS;
}2.4 編譯
* [root@bogon hello]# [root@localhost person]# ./configure && make && make install
2.5 擴展安裝
改更php.ini 加上
[person] extenstion=person.so
2.6 擴展使用
[root@bogon tests]# cat test.php <?php $n = new person(); echo $n->saying(); echo $n->doing(); [root@localhost tests]# php test.phpconstructsayingdoingdestruct
以上是“php7擴展類怎么實現”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。