略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: Lua::registerCallback

2024-11-12

Lua::registerCallback

(No version information available, might only be in Git)

Lua::registerCallback向Lua中注册php函数

说明

public Lua::registerCallback(string $name, callable $function): mixed

向Lua注册php函数,函数名为"$name"

参数

name

function

一个有效的PHP回调函数

返回值

成功返回$this,参数错误返回null ,其它错误返回false

范例

示例 #1 Lua::registerCallback()示例

<?php
$lua 
= new Lua();
$lua->registerCallback("echo""var_dump");
$lua->eval(<<<CODE
    echo({1, 2, 3});
CODE
);
?>

以上例程会输出:

array(3) {
  [1]=>
  float(1)
  [2]=>
  float(2)
  [3]=>
  float(3)
}
add a noteadd a note

User Contributed Notes 1 note

up
0
turn_and_turn at sina dot com
2 years ago
// init lua
$lua = new Lua();

/**
* Hello world method
*/
function helloWorld()
{
    return "hello world";
}

// register our hello world method
$lua->registerCallback("helloWorld", helloWorld);
$lua->eval("
    -- call php method
    local retVal = helloWorld()

    print(retVal)
");

// register our hello world method but using an other name
$lua->registerCallback("worldHello", helloWorld);

// run our lua script
$lua->eval("
    -- call php method
    local retVal = worldHello()

    print(retVal)
");

官方地址:https://www.php.net/manual/en/lua.registercallback.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3