• Stars
    star
    923
  • Rank 47,542 (Top 1.0 %)
  • Language
    Go
  • License
    MIT License
  • Created over 8 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

PHP bindings for the Go programming language (Golang)

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.

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.

More Repositories

1

grawkit

The Awksome Git Graph Generator
Awk
167
star
2

coreos-home-server

Home Server Setup with CoreOS
Dockerfile
35
star
3

fawkss

Fawkss is a CSS preprocessor for people who dislike CSS preprocessors
Awk
30
star
4

dotfiles

Configuration file management for the 21st century
Emacs Lisp
15
star
5

kube-home-server

Kubernetes on a Home Server. Have since moved to https://github.com/deuill/coreos-home-server as a daily driver.
Dockerfile
13
star
6

mash

The simple task runner
Go
5
star
7

vector-watch-hacking

Reverse-Engineering the Vector Watch
5
star
8

shell-extension-quitfromdash

Adds a quit option to Dash Pop-Over Context Menus, with support for Gnome Shell 3.10. Forked from cldx3000's version.
JavaScript
4
star
9

farsight

Fetch, filter and store arbitrary data using struct types and tags
Go
3
star
10

spooky-maze

Spooky Maze is a remake of the PSX/Net Yaroze game "Haunted Maze".
C
3
star
11

informbot

A Chat Bot for Inform 7 Stories, Built in Go
Go
3
star
12

cecil

Cecil is the de facto CMS for the Sleepy web framework.
PHP
2
star
13

sleepy-client

Sleepy is a web framework using a client - server architecture. This is the client part.
PHP
2
star
14

bran

A zero-configuration statusbar for i3
Go
1
star
15

empathy-theme-flat

A flat theme for Empathy (and possibly Adium).
CSS
1
star
16

xmpp-xep-dash-docset

Dash Docset generator for XMPP XEPs. Just add water.
Makefile
1
star
17

sleepy

Sleepy is a web framework using a client - server architecture. This is the server part.
Go
1
star
18

sleepy-app

Sleepy is a web framework using a client - server architecture. This is a starter template for projects based on Sleepy.
PHP
1
star
19

sigil

Sigil is web framework with bindings for various dynamic languages.
Go
1
star