Edit me

Getting started

Requirements#

Phel requires PHP 8.2 or higher and Composer.

Quick start with a scaffolding template#

To get started right away, you can create a new Phel commandline project via Composer's create-project command:

composer create-project --stability dev phel-lang/cli-skeleton example-app

Once the project has been created, start the REPL (read-evaluate-print loop) to try Phel.

cd example-app
composer repl

Alternatively to the phel-lang/cli-skeleton, you can also use phel-lang/web-skeleton for a web project. More information can be found in the README of the project.

Manually initialize a new project using Composer#

The easiest way to get started is by setting up a new Composer project. First, create a new directory and initialize a new Composer project.

mkdir hello-world
cd hello-world
composer init

Next, require Phel as a dependency.

composer require phel-lang/phel-lang

Optionally, you can create phel-config.php at the root of the project:

<?php

return (new \Phel\Config\PhelConfig())
    ->setSrcDirs(['src']);

Read the docs to see all available configuration options for Phel.

Then, create a new directory src with a file main.phel inside this directory.

mkdir src

The file main.phel contains the actual code of the project. It defines the namespace and prints "Hello, World!".

# inside `src/main.phel`
(ns hello-world\main)

(println "Hello, World!")

Running the code#

There are two ways to run the code: from the command line and with a PHP Server.

From the Command line#

Code can be executed from the command line by calling the vendor/bin/phel run command, followed by the file path or namespace:

vendor/bin/phel run src/main.phel
# or
vendor/bin/phel run hello-world\\main
# or
vendor/bin/phel run "hello-world\main"

The output will be:

Hello, World!

With a PHP Server#

Check the web-skeleton project on GitHub.

The file index.php will be executed by the PHP Server. It initializes the Phel Runtime and loads the namespace from the main.phel file described above, to start the application.

// src/index.php
<?php

use Phel\Phel;

$projectRootDir = __DIR__ . '/../';

require $projectRootDir . 'vendor/autoload.php';

Phel::run($projectRootDir, 'hello-world\\main');

The PHP Server can now be started.

# Start server
php -S localhost:8000 ./src/index.php

In the browser, the URL http://localhost:8000 will now print "Hello, World!".

When using a web server, consider building the project to avoid compilation time for each request; so PHP will run the transpiled PHP code instead to gain performance. See more Buid the project.

Launch the REPL#

To try Phel you can run a REPL by executing the ./vendor/bin/phel repl command.

Read more about the REPL in its own chapter.

Editor support#

Phel comes with basic support for VSCode, PhpStorm, a Emacs mode with interactive capabilities and a Vim plugin in the making.