Installation

Code Slicer requires PHP 8.4 or later and the Tokenizer extension. Composer installs its alto/language dependency automatically.

Install

Run this command in your project directory:

composer require alto/code-slicer

Verify the installation

Create check.php beside the vendor directory:

<?php

require __DIR__.'/vendor/autoload.php';

use Alto\Code\Slicer\CodeSource;

$slice = CodeSource::fromString("first\nsecond")->lines(2, 2);
echo $slice->content(), "\n";

Run php check.php. The result should be:

second

Framework applications usually load Composer's autoloader already. Standalone scripts need the require statement shown above.

Continue with Getting started to extract a PHP method.