mirror of
https://github.com/deuill/go-php.git
synced 2024-09-21 08:50:45 +00:00
48 lines
1.8 KiB
Markdown
48 lines
1.8 KiB
Markdown
|
# PHP bindings for Go
|
||
|
|
||
|
This package implements bindings for calling PHP scripts and binding Go variables for use in PHP contexts.
|
||
|
|
||
|
## Building
|
||
|
|
||
|
Building this package requires that you have PHP built and installed as a library. For most Linux systems, this would be a file under `/usr/lib/libphp5.so`, and usually exists in a package named `php5-embed`, or variations thereof.
|
||
|
|
||
|
Be aware that, by default, PHP is *not* designed to be used in multithreaded environments (which restricts the use of goroutines) if not built with [ZTS support](https://secure.php.net/manual/en/pthreads.requirements.php). Unfortunately, most distributions do not ship ZTS-enabled versions of PHP by default, and very few offer pre-compiled packages, so you're mostly left to compile one yourself.
|
||
|
|
||
|
That said, the Go PHP bindings *can* be used in Goroutines, with caution. Synchronization is left to the user.
|
||
|
|
||
|
Once the PHP library is available, the bindings can be compiled with `go build` and are `go get`-able.
|
||
|
|
||
|
## Usage
|
||
|
|
||
|
Executing a script is very simple:
|
||
|
|
||
|
```go
|
||
|
package main
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"os"
|
||
|
|
||
|
php "github.com/deuill/go-php"
|
||
|
)
|
||
|
|
||
|
func main() {
|
||
|
var b bytes.Buffer
|
||
|
|
||
|
engine, _ := php.New()
|
||
|
context, _ := engine.NewContext(&b)
|
||
|
|
||
|
context.Exec("src/index.php")
|
||
|
b.WriteTo(os.Stdout)
|
||
|
}
|
||
|
```
|
||
|
|
||
|
The above will execute script file located in `src/index.php` and write any output to the `io.Writer` passed to `engine.NewContext`.
|
||
|
|
||
|
By executing the PHP context in a goroutine, one can read from the writer as the output is generated by the script, and is useful for long-running scripts which output information during execution.
|
||
|
|
||
|
## Status/Caveats
|
||
|
|
||
|
Currently, the bindings are incomplete and offer no variable binding capabilities. This functionality is underway, however.
|
||
|
|
||
|
State of tests and documentation are also terribly lacking.
|