Introduction

Immutable 2D, 3D, 4D and n-dimensional vectors for PHP. Every vector is a readonly value object made of floats.

Requirements#

The library is plain PHP and has no runtime dependencies.

Installation#

Install the library with Composer.

composer require phpmath/vector

A first example#

Create a vector from its components, then combine it with another one. A vector is a list of numbers, for example a point or a direction in space.

use PHPMath\Vector\Vector3;

$a = new Vector3(1, 0, 0);
$b = new Vector3(0, 1, 0);

$sum = $a->add($b);

echo $sum; // [1, 1, 0]
echo $a;   // [1, 0, 0]

Calling add() did not change $a. It returned a new Vector3. This is true for every operation in the library.

Where to go next#

Start with the core concepts, or jump straight to the topic you need.