我是靠谱客的博主 小巧鸭子,最近开发中收集的这篇文章主要介绍go 头 php,go-php,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

PHP bindings for Go

68747470733a2f2f676f646f632e6f72672f6769746875622e636f6d2f646575696c6c2f676f2d7068703f7374617475732e73766768747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e737667

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.

Both PHP 5.x and PHP 7.x series are supported.

Building

Building this package requires that you have PHP installed as a library. For most Linux systems, this can usually be found in the php-embed package, or variations thereof.

Once the PHP library is available, the bindings can be compiled with go build and are go get-able.

Note: Building against PHP 5.x requires that the php5 tag is provided, i.e.:

go get -tags php5 github.com/deuill/go-php

This is due to the fact that PHP 7.x is the default build target.

Status

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).

Caveats

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.

Roadmap

Currently, the package lacks in several respects:

ZTS/multi-threading support. This basically means using Go-PHP in Goroutines is severely limited.

Documentation and examples, both package-level and external.

Performance. There's no reason to believe Go-PHP suffers from any serious performance issues in particular, but adding benchmarks, especially compared against vanilla PHP, might help.

Your feature request here?

These items will be tackled in order of significance (which may not be the order shown above).

Usage

Basic

Executing a script is simple:

package main

import (

php "github.com/deuill/go-php"

"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).

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, _ := 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).

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.

最后

以上就是小巧鸭子为你收集整理的go 头 php,go-php的全部内容,希望文章能够帮你解决go 头 php,go-php所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(46)

评论列表共有 0 条评论

立即
投稿
返回
顶部