PHPackages                             quasilyte/klua - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. quasilyte/klua

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

quasilyte/klua
==============

FFI-based Lua5 library that can be used in both PHP and KPHP

v1.0.0(3y ago)87MITPHPPHP &gt;=7.4

Since Aug 12Pushed 3y ago2 watchersCompare

[ Source](https://github.com/quasilyte/KLua)[ Packagist](https://packagist.org/packages/quasilyte/klua)[ RSS](/packages/quasilyte-klua/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (5)Versions (2)Used By (0)

[![](docs/logo.png)](docs/logo.png)

KLua
====

[](#klua)

KLua is a [FFI](https://www.php.net/manual/ru/class.ffi.php)-based [Lua5](https://www.lua.org) library that can be used in both PHP and [KPHP](github.com/VKCOM/kphp).

Installation
------------

[](#installation)

Since this is a FFI library, it needs a dynamic library available during the run time.

Installation steps:

1. Install liblua5 in your system (if you don't have it already)
2. Locate the library file and place in under `./ffilibs/liblua5`
3. Install this composer package to use KLua classes inside your code

Depending on your system, you need to find `liblua.so`, `liblua.dylib`or `liblua.dll` file. Then you can copy it to the application root `ffilibs` folder under the `liblua5` name (note: no extension suffixes).

If you're having difficulties locating the library file, use a helper script:

```
$ php -f locate_lib.php
note: can't locate liblua5.4, maybe it's not installed
library candidate: /lib/x86_64-linux-gnu/liblua5.3.so.0
library candidate: /lib/x86_64-linux-gnu/liblua5.3.so

run something like this to make it discoverable (unix):
	mkdir -p ffilibs && sudo ln -s /lib/x86_64-linux-gnu/liblua5.3.so ./ffilibs/liblua5.3
```

Then install the composer library itself:

```
$ composer require quasilyte/klua
```

Notes:

- If you want to place library files/links globally, make `./ffilibs` a symlink
- You'll probably want to add `ffilibs/` to your gitignore

Examples
--------

[](#examples)

- [simple.php](examples/1_simple.php) - a simple overview of the API basics
- [phpfunc.php](examples/2_phpfunc.php) - how to bind PHP functions to Lua
- [override\_print.php](examples/3_override_print.php) - override Lua `print()` stdlib function
- [limited\_stdlib.php](examples/4_limited_stdlib.php) - how to limit the stdlib access in Lua scripts
- [plugin\_sandbox.php](examples/5_plugin_sandbox.php) - how to load several plugins without conflicts
- [phpfunc\_table.php](examples/6_phpfunc_table.php) - how to create module-like native libraries
- [userdata.php](examples/7_userdata.php) - how to use Lua [light userdata](https://www.lua.org/pil/28.5.html)
- [memory\_limit.php](examples/8_memory_limit.php) - how to limit the amount of memory Lua scripts can use
- [time\_limit.php](examples/9_time_limit.php) - how to limit the amount of time Lua scripts can run

Running examples with PHP:

```
$ php -d opcache.enable_cli=1\
      -d opcache.preload=preload.php\
      -f ./examples/1_simple.php
```

Running examples with KPHP:

```
# Step 1: compile the example:
$ kphp --mode cli --composer-root $(pwd) ./examples/simple.php
# Step 2: run the binary:
$ ./kphp_out/cli
```

Quick start
-----------

[](#quick-start)

```
