-
Notifications
You must be signed in to change notification settings - Fork 0
/
2_phpfunc.php
37 lines (27 loc) · 963 Bytes
/
2_phpfunc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php
require_once __DIR__ . '/../vendor/autoload.php';
use KLua\KLua;
use KLua\KLuaConfig;
if (KPHP_COMPILER_VERSION) { KLua::loadFFI(); }
KLua::init(new KLuaConfig());
// Use KLua::registerFunction to make PHP code callable from Lua.
// The number suffix specifies how many parameters your PHP function has.
KLua::registerFunction1('php_json_encode', function ($value) {
return json_encode($value);
});
// You can use conventional "function references"
// and even static/instance method references here.
KLua::registerFunction1('php_json_decode', 'php_json_decode');
/** @kphp-required */
function php_json_decode($s) {
return json_decode($s, true);
}
// Registered PHP functions are now accessible via Lua code.
KLua::eval('
table_encoded = php_json_encode({1, 2, 3})
table_decoded = php_json_decode(table_encoded)
');
// => "[1,2,3]"
var_dump(KLua::getVar('table_encoded'));
// => [1, 2, 3]
var_dump(KLua::getVar('table_decoded'));