Home  >  Article  >  Backend Development  >  How to write PHP extension in golang

How to write PHP extension in golang

藏色散人
藏色散人forward
2021-03-02 15:46:163926browse

The following tutorial column of golang will introduce to you how to write PHP extension methods in golang. I hope it will be helpful to friends who need it!

golang Write PHP extension

phper all know that php-extension is written in C/C.
We use C/C due to development efficiency issues Golang writes php-extension
Make full use of golang's development efficiency and ecology to contribute to PHP.

Environment preparation

wsl ubuntu
golang 1.13
php 7.3

New project

php7.3 里面的 ext_skel 和以前版本参数不一样,请查看 help 操作
php:
./ext_skel.php --ext goserver --onlyunix
golang:
 mkdir goserver

Encapsulation file

Encapsulation C file into golang file

package main
/*
 php扩展.C文件内容
*/
import "C"    
func main() {}

build

$ phpize
$ ./configure
$ CGO_CFLAGS="-g -O2 -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1 \  
-I. -DPHP_ATOM_INC \
-I/usr/include/php/20180731/include \  
-I/usr/include/php/20180731/main \
-I/usr/include/php/20180731 \
-I/usr/include/php/20180731/main \  
-I/usr/include/php/20180731/TSRM \  
-I/usr/include/php/20180731/Zend \  
-I/usr/include/php/20180731/ext \  
-I/usr/include/php/20180731/ext/date/lib \  
-DHAVE_CONFIG_H -DCOMPILE_DL_GOPHP -fPIC -DPIC" \  
CGO_LDFLAGS="-Wl,--export-dynamic -Wl,--unresolved-symbols=ignore-all -Wl,-z,nodelete" \  
go build -p 1 -gcflags "-l" -buildmode=c-shared -o modules/goserver.so goserver.go

CGO compilation parameters Fill in according to the actual situation, you can check the parameters in the generated makefile file

run

Modify php.ini and add extension=goserver.so
Run the extension that comes with it Function test


#

The above is the detailed content of How to write PHP extension in golang. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete