Skip to content
arnaud-lbPublic

About

php-go allows to call Go code from PHP, with minimal code boilerplate

Topics

Resources

Stars

191 stars

Watchers

11 watching

Forks

Latest commit

 

History

35 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

php-go

Supported PHP versions: 7.x Build Status

php-go allows to call Go code from PHP, with minimal code boilerplate.

Goals:

  • 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

TODO:

  • Support exporting functions
  • Support all scalar types in arguments and return values
  • Support exporting constants
  • Support slices, maps (copying)
  • Support objects (proxying)

Install

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.

Usage

Exporting Go functions

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.

Using the module in PHP

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

About

php-go allows to call Go code from PHP, with minimal code boilerplate

Topics

Resources

Stars

191 stars

Watchers

11 watching

Forks

Releases

Packages

Contributors

Languages