Skip to content
/ php-go Public
forked from sobomax/php-go

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

License

Notifications You must be signed in to change notification settings

schivei/php-go

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

49 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

php-go

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

DOING:

  • Support GoLang Http Requests

Install

You can download this package using "go install". Then you can run:

go install github.com/schivei/php-go

NOTES:

  • You need to have GoLang installed in your machine.
  • php-go also requires PHP development headers to be installed.
  • php-go needs CGO to be enabled.

When this is finished, go to your project directory and run:

php-go

You also can pass binary directory as argument:

php-go /path/to/bin

The extension will be built and placed in the current directory.

Then copy the resulting phpgo.so to your PHP extensions directory and add extension=phpgo.so to your php.ini.

Alternatively, you can use phpgo.so from the bin directory.

Note: php-go supports PHP 8+ (non-ZTS).

Also, you can use php composer to install this package:

composer require schivei/php-go

It will install the package and the extension will be placed in the vendor/schivei/php-go directory.

Usage

Exporting Go functions

package main

import (
  "strings"
  "github.com/schivei/php-go/php"
)

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

// Call some method
$module->toUpper("foo");

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C 49.4%
  • Go 33.4%
  • PHP 15.9%
  • Other 1.3%