CakeFest 2024: The Official CakePHP Conference

Lua::registerCallback

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

Lua::registerCallbackPHP 関数を Lua に登録

説明

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

PHP 関数を Lua に "$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 note

User Contributed Notes 1 note

up
0
turn_and_turn at sina dot com
4 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)
");
To Top