https://github.com/arnaud-lb/php-go
php-go allows to call Go code from PHP, with minimal code boilerplate
https://github.com/arnaud-lb/php-go
go golang php php-go
Last synced: about 1 month ago
JSON representation
php-go allows to call Go code from PHP, with minimal code boilerplate
- Host: GitHub
- URL: https://github.com/arnaud-lb/php-go
- Owner: arnaud-lb
- License: mit
- Created: 2015-12-26T22:09:00.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2020-07-29T04:25:07.000Z (almost 5 years ago)
- Last Synced: 2024-06-20T10:12:50.586Z (12 months ago)
- Topics: go, golang, php, php-go
- Language: C
- Homepage:
- Size: 59.6 KB
- Stars: 186
- Watchers: 13
- Forks: 19
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# php-go
 [](https://travis-ci.org/arnaud-lb/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## TODO:
- [x] Support exporting functions
- [x] 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](https://github.com/golang/go/wiki/SettingGOPATH) 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
``` go
package mainimport (
"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
``` 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");
```