Lua

Lua for PHP extension

Lua extension embeds the lua interpreter and offers an OO-API to lua variables and functions.

Download

PECL: lua

APIS

   $lua = new Lua($lua_script_file = NULL);
   
   $lua->eval("lua_statements");     //eval lua codes
   $lua->include("lua_script_file"); //import a lua script
 
   $lua->assign("name", $value); //assign a php variable to Lua
   $lua->registerCallback("name", $function); //register a PHP function to Lua with "name"
 
   $lua->call($string_lua_function_name, array() /*args*/);
   $lua->call($resouce_lua_anonymous_function, array() /*args);
   $lua->call(array("table", "method"), array(...., "push_self" => [true | false]) /*args*/);
 
   $lua->{$lua_function}(array()/*args*/);

   
   

Compile Lua in *nix

$phpize
$configure --with-php-config=/path/to/php-config  --with-lua=/path/to/lua/
make && make install

Compile Lua in Win32

Just define the CFLAGS = -I/path/to/lua/ -L/path/to/lua -llua

Example

test.lua

globalname = "From Lua";
function test()
    print(from);
    name = "laruence";
    comp = "baidu"
    callphp(name, comp);
    return (function() print(name, ":", globalname, "\n") end);
end

lua.php

<?php
$lua = new Lua("/tmp/test.lua");
 
$a = new stdclass();
$a->foo = "bar";
 
class A {
   public static function intro($name, $company) {
    var_dump(func_get_args());
  }
}
 
$lua->assign("from", $a); /** assign a PHP var to Lua named from */
 
$lua->registerCallback("callphp", array("A", "intro")); /** register a php function to Lua named callphp */
 
$func = $lua->test(); /** call Lua function and get return closure */
 
var_dump($lua->globalname); /** echo a Lua variable named globalname */
 
$lua->globalname = "From PHP"; /** set a PHP string to Lua global variable named globalname */
 
$lua->call($func); /** call Lua function closure */

var_dump($lua->eval(<<<LUA
	return 1,3,4,5,6,7;
LUA
));
 
var_dump(Lua::LUA_VERSION); /** echo the Lua version */
 
var_dump($lua->call(array("string", "lower"), array("AAAA"))); /** call Lua table function string.lower */
 
var_dump($lua->call(array("table", "concat")
    , array(array(12,3,21,3,24,32,4,2,5,435,35), ","))); /** call Lua table function table.concat */
       
//get a clousre return from lua
$lua_closure = $lua->eval(<<<LUA
       return (function (from)
          print(from);
       end);
LUA
); 

try {
     $lua->call($lua_closure, array("php"));
     $lua_closure->invoke("-");
     $lua_closure("5.3.7");//only works after PHP 5.3
} catch (LuaException $e) {
     echo $e->getMessage();
}

output

Array
 (
     [foo] => bar
 )
array(2) {
  [0]=>
  string(8) "laruence"
  [1]=>
  string(5) "baidu"
}
string(8) "From Lua"
The Length of stack is 1
function:
laruence:From PHP
array(6) {
  [0]=>
  float(1)
  [1]=>
  float(3)
  [2]=>
  float(4)
  [3]=>
  float(5)
  [4]=>
  float(6)
  [5]=>
  float(7)
}
string(9) "Lua 5.1.4"
string(4) "aaaa"
PHP Notice:  Lua::call(): attempt to pass an array index begin with 0 to lua, the index 0 will be discarded in /tmp/1.php on line 35

Notice: Lua::call(): attempt to pass an array index begin with 0 to lua, the index 0 will be discarded in /tmp/1.php on line 35
string(25) "3,21,3,24,32,4,2,5,435,35"
The Length of stack is 2
function:
function:
phplaruence:From PHP
laruence:From PHP
destructdestruct