Giter Site home page Giter Site logo

charm-jp / php-go Goto Github PK

View Code? Open in Web Editor NEW

This project forked from arnaud-lb/php-go

0.0 1.0 0.0 63 KB

php-go allows to call Go code from PHP, with minimal code boilerplate

License: MIT License

Go 21.34% C 61.96% PHP 13.14% Shell 1.68% Makefile 0.35% M4 0.76% JavaScript 0.77%

php-go's Introduction

php-go

Supported PHP versions: 7.x Build Status

php-go allows to call Go code from PHP, with minimal code boilerplate.

Goals:

  • Allow to export Go functions and Go constants from Go to PHP
  • Be reliable and always safe
  • Deploy Go code without re-building the PHP extension

TODO:

  • Support exporting functions
  • Support all scalar types in arguments and return values
  • Support exporting constants
  • Support slices, maps (copying)
  • Support objects (proxying)

Install

You can download this package using "go get". When using "go get", you'll have to set your $GOPATH first. Then you can run:

go get github.com/arnaud-lb/php-go

When this is finished, change directories to the included "ext" folder:

cd $GOPATH/src/github.com/arnaud-lb/php-go/ext

Then configure and make the binary:

phpize && ./configure && make && sudo make install

Then add extension=phpgo.so to your php.ini, or call php with -dextension=phpgo.so

Note: php-go supports PHP 7 (non-ZTS). For PHP 5, use the php5 branch.

Usage

Exporting Go functions

package main

import (
  "strings"
  "github.com/arnaud-lb/php-go/php-go"
)

// call php.Export() for its side effects
var _ = php.Export("example", map[string]interface{}{
  "toUpper": strings.ToUpper,
  "takeOverTheWorld": TakeOverTheWorld,
})

func TakeOverTheWorld() {
}

func main() {
}

The module can then be compiled as a shared library using -buildmode c-shared:

go build -o example.so -buildmode c-shared .

Note: Go requires that the module be a main package with a main function in this mode.

Using the module in PHP

// Create a class from the Go module, and return an instance of it
$module = phpgo_load("/path/to/example.so", "example");

// Display the methods defined by the class
ReflectionClass::export($module);

// Call some method
$module->toUpper("foo");

php-go's People

Contributors

arnaud-lb avatar dasjott avatar roelofjan-elsinga avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.