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

How to write PHP extension in golang

藏色散人
Release: 2021-03-02 15:46:16
forward
3975 people have browsed it

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
Copy after login

New project

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

Encapsulation file

Encapsulation C file into golang file

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

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
Copy after login

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!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template