Download the PHP package lisachenko/native-php-matrix without Composer

On this page you can find all versions of the php package lisachenko/native-php-matrix. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package native-php-matrix

# 🧮 Native PHP Matrix ### Real operator overloading for PHP — in pure PHP. **Native PHP Matrix** gives PHP a `Matrix` type with genuine arithmetic: `+`, `-`, `*`, `/`, `**` and `==` all work on matrices the way they work on numbers. There is no C code, no PECL package, no compiler — just [Z-Engine](https://github.com/lisachenko/z-engine) hooking the Zend Engine's own `do_operation` and `compare` handlers through FFI. [![CI](https://img.shields.io/github/actions/workflow/status/lisachenko/native-php-matrix/ci.yml?branch=master&label=CI)](https://github.com/lisachenko/native-php-matrix/actions/workflows/ci.yml) [![GitHub release](https://img.shields.io/github/release/lisachenko/native-php-matrix.svg)](https://github.com/lisachenko/native-php-matrix/releases/latest) [![PHP Version](https://img.shields.io/badge/php-8.4%20%7C%208.5-8892BF.svg)](https://php.net/) [![License](https://img.shields.io/packagist/l/lisachenko/native-php-matrix.svg)](https://packagist.org/packages/lisachenko/native-php-matrix) [![PHPStan](https://img.shields.io/badge/PHPStan-level%20max-brightgreen.svg)](https://phpstan.org/)

⚠️ Experimental — educational first, production second. This library installs custom object handlers into a running Zend Engine. It is a demonstration of what userland PHP can reach, and it inherits every constraint of the machinery underneath it. Pin your PHP version, keep it out of production until there are stable tags on both this package and Z-Engine, and treat a crash as an engine-level bug report rather than a flaky test.

The first PHP "extension" written without C

For a long time operator overloading was the exclusive privilege of compiled extensions: write C, learn the zend_object_handlers struct, build a .so, ship a PECL release. This library is the first ever userland PHP extension that implements true operator overloading — and it does it entirely in PHP.

The trick is lisachenko/z-engine, which loads version-exact FFI definitions of the engine's C structures and lets PHP write into them. Matrix installs its own do_operation and compare handlers on its own class entry, so when the VM evaluates $a * $b and sees an object operand, it calls straight back into PHP code. The operators are not simulated, not parsed, not wrapped in a fluent API — the engine really dispatches them.

What you get

Operation Example
➕ Addition — element-wise, dimensions must match $a + $b
➖ Subtraction — element-wise, dimensions must match $a - $b
✖️ Matrix multiplication — columns of the left must match rows of the right $a * $b
🔢 Scalar multiplication — in both directions $a * 2 and 2 * $a
➗ Scalar division $a / 2
🔺 Scalar exponentiation — element-wise power $a ** 2
🟰 Equality — strict element-wise comparison $a == $b, $a != $b
🛡️ Type-safe by design — PHPStan generics Matrix<int> / Matrix<float>, checked at level max Matrix<int|float>

Every operation returns a new Matrix; the class is final and its state is readonly, so nothing is ever mutated in place. The constructor validates that you passed a rectangular list of rows holding only int/float cells and raises a catchable InvalidArgumentException when it does not.

Operator-level failures are a different story, and it is worth being blunt about it: a dimension mismatch (InvalidArgumentException) or an unimplemented operand combination such as $a - 2 (LogicException) is raised inside an FFI callback, and PHP does not allow an exception to cross that boundary. The engine prints the exception and then halts with Fatal error: Throwing from FFI callbacks is not allowed. You cannot try/catch it — check your dimensions before you multiply.

Generics are honest about arithmetic: Matrix<int> divided by 2 is a Matrix<int|float>, because PHP's / widens. Nothing pretends to preserve T where the maths does not.

How it works

Three moving parts, in order:

  1. bootstrap.php is registered in Composer's files autoload, so it runs the moment you require vendor/autoload.php. It calls ZEngine\Core::init(), which validates that the FFI struct definitions match the exact PHP build you are running.
  2. It then calls installExtensionHandlers() on ZEngine\Reflection\ReflectionClass for Matrix, wiring the class's create_object, do_operation and compare slots to the engine trampolines. Order matters — create_object allocates the memory the other handlers live in.
  3. Matrix::__doOperation() and Matrix::__compare() are static hooks. The engine hands them a DoOperationHook / CompareValuesHook describing the opcode and both operands; they dispatch to ordinary, boring, pure-PHP methods (sum(), subtract(), multiply(), multiplyByScalar(), divideByScalar(), powByScalar(), equals()).

The maths is plain PHP. The magic is only in getting the engine to call it.

Requirements

Installation

This package requires Z-Engine as ~8.4.2 || ~8.5.0 — z-engine minors track PHP minors and are not interchangeable, so Composer resolves the release line matching your PHP automatically (8.4.x on PHP 8.4, 8.5.x on PHP 8.5). Both are stable tags, so no minimum-stability tweak is needed for them.

native-php-matrix itself is not tagged yet, which is why the command above asks for dev-master; that one dev constraint is explicit, so Composer accepts it without a root stability change. Once a native-php-matrix release is tagged, this collapses to a plain composer require lisachenko/native-php-matrix.

No initialization call is needed: bootstrap.php ships in the package's files autoload and sets everything up behind require vendor/autoload.php.

Usage

The classic

Operator precedence is the engine's own, so * binds tighter than + exactly as you would expect from numbers.

Matrix by matrix

Powers and division

Comparison

Equality is element-wise and strict. Ordering operators (<, >) are deliberately not meaningful for matrices — the compare hook reports "unordered" rather than inventing a total order.

Casting

Casts are dispatched by the engine too — the cast_object and get_properties_for handlers:

Numeric casts keep the engine's default behaviour ((int) $m gives 1), and everything that is not a cast — var_dump(), serialize(), var_export(), json_encode(), get_object_vars() — still sees the object exactly as before.

Beyond operators

The class is a normal PHP object too:

Roadmap

Tracked as GitHub issues — contributions welcome:

Contributing

The repository ships an agent/contributor contract in CLAUDE.md — read it before changing anything, especially the section on why the PHP version is pinned.

License

Released under the MIT License.


All versions of native-php-matrix with dependencies

PHP Build Version
Package Version
Requires php Version ^8.4
ext-ffi Version *
lisachenko/z-engine Version ~8.4.2 || ~8.5.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package lisachenko/native-php-matrix contains the following files

Loading the files please wait ...