ホームページ  >  記事  >  バックエンド開発  >  php7拡張クラスの書き方

php7拡張クラスの書き方

醉折花枝作酒筹
醉折花枝作酒筹転載
2021-06-30 09:16:372444ブラウズ

php7には拡張クラスがたくさんありますが、今回はpersonクラスを例にdoingメソッドとsayingメソッドを実装していきますので、困っている友達は参考にしてみてください。

php7拡張クラスの書き方

1. 実装する必要がある詳細

人物クラスの実装

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);

を追加

/** * 声明构造函数 * 
@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をコンパイル

[person] extenstion=person.so

2.5 Extensionのインストール

# 変更php.ini を追加し、

[root@bogon tests]# cat test.php
<?php
$n = new person();
echo $n->saying();
echo $n->doing();
[root@localhost tests]# php test.phpconstructsayingdoingdestruct

2.6 拡張使用

rrreee 推奨学習: php ビデオ チュートリアル

###

以上がphp7拡張クラスの書き方の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はimooc.comで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。