这个包实现了对执行PHP脚本、导出Go变量以便在PHP上下文中使用、将Go方法接收器附加为PHP类以及返回PHP变量以便在Go上下文中使用的支持。
支持PHP 5.x and PHP 7.x
构建这个包需要将PHP作为库安装。对于大多数Linux系统,这通常可以在“php-embed”包或其变体中找到。 确保PHP库可用,就可以使用“go build”编译绑定,并且可以使用“go get”。
Note: Building against PHP 5.x requires that the php5
tag is provided, i.e.:
go get -tags php5 gitee.com/shirdonl/php-go
This is due to the fact that PHP 7.x is the default build target.
Executing PHP [script files] as well as [inline strings]is supported and stable.
[Binding Go values] as PHP variables is allowed for most base types, and PHP values returned from eval'd strings can be converted and used in Go contexts as interface{}
values.
It is possible to [attach Go method receivers] as PHP classes, with full support for calling expored methods, as well as getting and setting embedded fields (for struct
-type method receivers).
Be aware that, by default, PHP is not designed to be used in multithreaded environments (which severely restricts the use of these bindings with Goroutines) if not built with ZTS support. However, ZTS support has seen major refactoring between PHP 5 and PHP 7, and as such is currently unsupported by this package.
Currently, it is recommended to either sync use of seperate Contexts between Goroutines, or share a single Context among all running Goroutines.
Currently, the package lacks in several respects:
These items will be tackled in order of significance (which may not be the order shown above).
Executing a script is simple:
package main
import (
php "gitee.com/shirdonl/php-go"
"os"
)
func main() {
engine, _ := php.New()
context, _ := engine.NewContext()
context.Output = os.Stdout
context.Exec("index.php")
engine.Destroy()
}
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).
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 "gitee.com/shirdonl/php-go"
)
func main() {
engine, _ := php.New()
context, _ := engine.NewContext()
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.
engine.Destroy()
}
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).
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.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。
1. 开源生态
2. 协作、人、软件
3. 评估模型