Press "Enter" to skip to content

PLua - Lua for PHP

上周的时候, 搞mysql proxy, 发现要用lua写服务器脚本, 加之以前配置lighttpd的时候, 配置也可以用lua来写, 就想彻底学习和研究下lua.
本着学习Lua的态度, 写了一个PHP扩展Plua, 把Lua解析器嵌入了PHP.
Lua的堆栈式传参, 很值得借鉴, 这点上, 感觉比PHP用一个结构体表示弱类型, 要来的更严格, 更可靠一些.
目前可以想到的应用场景, 是可以实现一种编写(Lua), 多处调用(C, PHP, Java等).
不废话了, 项目主页: Plua
代码在Google code上:

   $lua = new Plua($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->register("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*/);

后记: Plua项目现在已经取代了原来的Lua成为了PECL的Lua扩展, Plua也改名为了Lua, 所以请使用最新的Lua


  1. php大神
    php大神 September 16, 2021

    我发现了当使用了registerCallback注册一个变量给PHP之后,导致内存不被释放的问题(MyLua 和 Lua 都不会释放):

    class MyLua {
    protected $lua;

    public function __construct()
    $this->lua = new \Lua();
    $this->lua->registerCallback(‘import’, function() { return $this->xxx(…func_get_args()); });

    protected function xxx()
    return “hello”;

    function foo() {
    new MyLua();

    for($i = 0; $i foo();
    echo ” ” . memory_get_usage() / 1024 . “\n”;


    • php大神
      php大神 September 16, 2021


      function foo() {
      $lua = new \Lua();
      $lua->assign(‘xxx’, function() {});

      // 清理匿名函数空间

      $ref = new \ReflectionClass($lua);
      foreach ($ref->getProperties() as $property) {
      if($property->getName() === ‘_callbacks’) {
      $property->setValue($lua, null);

  2. green angelica adalah
    green angelica adalah June 7, 2016

    I enjoy reading an article that will make men and women think.
    Also, many thanks for permitting me to comment!

  3. lucifer
    lucifer March 6, 2015

    现在的游戏开发从大家口中了解到的情况是,游戏的后端都是用 java 或者 python , 没有用php 这个…….Laruence你怎么看…….

  4. samsung s2 t-mobile
    samsung s2 t-mobile November 20, 2014

    Great beat ! I would like to apprentice while you amend your website,
    how could i subscribe for a blog site? The account
    helped me a acceptable deal. I had been a little bit acquainted of this your broadcast offered bright clear concept

  5. shiny
    shiny June 19, 2011


  6. xLight
    xLight June 19, 2011


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.