go-php7

module
v0.0.0-...-d82352a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 28, 2016 License: MIT

README

Original library is at: https://github.com/deuill/go-php

中文文档:https://segmentfault.com/a/1190000007619087

This fork is for:

  • Support PHP7 only, so that we can embrace the value semantics of zval
  • Fix memory leaks by using zval instead of engine_value
  • Use golang http request as PHP input
  • Use golang http response writer as PHP output

PHP bindings for Go API Documentation MIT License

This package implements support for executing PHP scripts, exporting Go variables for use in PHP contexts, attaching Go method receivers as PHP classes and returning PHP variables for use in Go contexts.

Only PHP 7.x series is supported.

Usage

Basic

Executing a script is very simple:

package main

import (
    php "github.com/deuill/go-php"
    "os"
)

func main() {
    engine, _ := php.New()

	context := &Context{Output: os.Stdout}
    engine.RequestStartup(context)
    defer engine.RequestShutdown(context)

    context.Exec("index.php")
}

The above will execute script file index.php located in the current folder and will write any output to the io.Writer assigned to Context.Output (in this case, the standard output).

Binding and returning variables

The following example demonstrates binding a Go variable to the running PHP context, and returning a PHP variable for use in Go:

package main

import (
    "fmt"
    php "github.com/deuill/go-php"
)

func main() {
    engine, _ := php.New()
	context := &Context{}
    engine.RequestStartup(context)
    defer engine.RequestShutdown(context)

    var str string = "Hello"
    context.Bind("var", str)

    val, _ := context.Eval("return $var.' World';")
    fmt.Printf("%s", val.Interface())
    // Prints 'Hello World' back to the user.
}

A string value "Hello" is attached using Context.Bind under a name var (available in PHP as $var). A script is executed inline using Context.Eval, combinding the attached value with a PHP string and returning it to the user.

Finally, the value is returned as an interface{} using Value.Interface() (one could also use Value.String(), though the both are equivalent in this case).

License

All code in this repository is covered by the terms of the MIT License, the full text of which can be found in the LICENSE file.

Directories

Path Synopsis
Package engine provides methods allowing for the initialization and teardown of PHP engine bindings, off which execution contexts can be launched.
Package engine provides methods allowing for the initialization and teardown of PHP engine bindings, off which execution contexts can be launched.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL