1
0
mirror of https://github.com/deuill/go-php.git synced 2024-09-21 08:50:45 +00:00
go-php/README.md
Alex Palaistras bd0f41e3d5 Initial commit of entire workspace
Code was seperated out from the Sigil web application framework.
https://github.com/deuill/sigil
2015-09-17 22:22:07 +01:00

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.