php-go allows to call Go code from PHP, with minimal code boilerplate.
- Allow to export Go functions and Go constants from Go to PHP
- Be reliable and always safe
- Deploy Go code without re-building the PHP extension
- Support exporting functions
- Support all scalar types in arguments and return values
- Support exporting constants
- Support slices, maps (copying)
- Support objects (proxying)
You can download this package using "go get". When using "go get", you'll have to set your $GOPATH first. Then you can run:
go get github.com/arnaud-lb/php-go
When this is finished, change directories to the included "ext" folder:
cd $GOPATH/src/github.com/arnaud-lb/php-go/ext
Then configure and make the binary:
phpize && ./configure && make && sudo make install
Then add extension=phpgo.so
to your php.ini, or call php with -dextension=phpgo.so
Note: php-go supports PHP 7 (non-ZTS). For PHP 5, use the php5 branch.
package main
import (
"strings"
"github.com/arnaud-lb/php-go/php-go"
)
// call php.Export() for its side effects
var _ = php.Export("example", map[string]interface{}{
"toUpper": strings.ToUpper,
"takeOverTheWorld": TakeOverTheWorld,
})
func TakeOverTheWorld() {
}
func main() {
}
The module can then be compiled as a shared library using -buildmode c-shared
:
go build -o example.so -buildmode c-shared .
Note: Go requires that the module be a main package with a main function in this mode.
// Create a class from the Go module, and return an instance of it
$module = phpgo_load("/path/to/example.so", "example");
// Display the methods defined by the class
ReflectionClass::export($module);
// Call some method
$module->toUpper("foo");