Introducing Phikl: Apple PKL in PHP

Apple’s new programming language is ready to be used with PHP!

Alexandre Daubois
5 min readFeb 26, 2024
Photo by David Todd McCarty on Unsplash

Maybe you heard about it: Apple released a new programming language called Pkl in early February 2024. Pronounced pickle, the main purpose of this language is to make it easier to work with configurations. It is a high-level language, and it is designed to be easy to read and write.

In fact, this language was created in response to the shortcomings of the static configuration languages we all know: JSON, YAML and XML.

Apple’s team explains very well the advantages and disadvantages of these historical configuration languages. When you think about it, these languages weren’t designed for configuration in the first place. Pkl, on the other hand, was created with this very purpose in mind.

Here are some of the notable features that Pkl brings to the world of configuration files:

  • Data validation ;
  • Strong typing ;
  • Object programming concepts with classes and methods ;
  • A standard library ;
  • And many more!

All this also leads to great IDE integration, already available for IntelliJ, Neovim and VS code.

Pkl’s CLI tool lets you export Pkl files (or modules) in “classic” formats such as JSON, YAML, XML and others. Bindings in other languages already exist, enabling Pkl to be used in a snap in projects written in Kotlin, Go, Swift and Java.

But what about other languages? What initiatives is the community taking to advance Pkl adoption? Good news for PHP developers: the first bridge for our language is here!

Using Pkl with PHP

Seeing Pkl’s growing popularity, I thought that creating a bridge for my language of choice, PHP, would be both a great way to help the community take advantage of Pkl and an excellent exercise.

Initially, I set out to create an interpreter for this language, but after a day’s work I realized that I had (grossly) underestimated the task. So how do I go about it?

In the end, the solution is quite trivial: create a wrapper around the Pkl command-line tool and offer a PHP API to interact with it. Obviously, issues such as performance are at the heart of the matter. Features such as caching quickly became necessary. Let’s take as an example a fairly minimal Pkl file, defining a user’s configuration. In the real world, this would be a server configuration, for example, but let’s keep it simple:

Second step: define a data model in PHP to represent this configuration:

What’s next? All we have to do is use Phikl to map our configuration to this class, and we’re ready to go:

If many users were defined in the same Pkl module, you could access them by their name with the module->get('...') method.

It often happens that the name of a property in a configuration file is different from the name of the property in the class being mapped. The PklProperty attribute has been created for just this purpose. Let’s modify our user class to look like this:

The $userId and $userName will automatically be mapped by the field given in the attribute.

It is important to note that the bridge with Pkl has some limitations at the time of writing these lines. Indeed, Phikl uses the JSON output of the Pkl CLI to work. Because of how the Pkl language is defined, it is not always possible to represent everything in JSON thus leading to some incompatibilities. This is something that may be fixed in future versions of Phikl, but keep these limitations in mind. You can see these limitations explained in the Pkl language website.

Phikl Command Line Tool

Phikl is bundled with a command line tool that allows to do plenty of operations. Some commands are just wrappers around Pkl CLI, whereas others operate directly with the bridge itself.

The first command you may want to use is phikl install, which installs the latest supported version of the pkl command line tool. The default installation directory is vendor/bin/pkl. The pkl binary is necessary when in development mode. However, it can omitted when deploying your application if you cache your Pkl modules, which will see in a moment.

Example of running the phikl install command

Just like the Pkl CLI, phikl can be used to evaluate your modules. This allows to execute validation on your configuration in a CI for example. To do this, you can use the eval subcommand with the name of the modules you cant to eval: phikl eval module1.pkl config/module2.pkl.

Example of running the phikl eval command

Caching Modules Evaluation

Phikl comes with cache capabilities. This is important and must be done when deploying your application in production. Indeed, without cache, the Pkl tool is executed every time you need to evaluate a module. This is a heavy operation and pretty intensive. To avoid this, you have the possibility to pre-evaluate your modules and cache the result. Such operation is to be done when deploying your application somewhere.

To cache your modules evaluation, simply run the following command: phikl warmup. The command will look for all *.pkl files in your project and evaluate them. The result will be dumped to a cache file, .phikl.cache. Now, whenever you evaluate a module thanks to Phikl, the cache will be used. Of course, if the module is not cached, Pkl command line tool will be used to evaluate the module and add it to the cache. Also, if Phikl detects a stale entry in the cache, it’ll do its best to update it.

Example of running the phikl warmup command

You can also validate your cache with the phikl validate-cache command. It is a good way to know if your cache is staled before having cache misses. In case your cache is corrupted, an explicit error message is displayed:

Error message is displayed when the cache is corrupted

Phikl uses a PSR-16 compliant caching system. This means you may provide your own cache solution. What’s cool about it is that you may even use some systems like Redis to cache your module evaluations. Pretty neat, isn’t it?

Phikl is still in very early and active development. This means that there are so many things to do, improve and test with this tool. But this is the first step to being able to use Apple’s Pkl configuration language that looks so promising in our PHP web applications.

Even if you don’t use Pkl, it sure is something to keep an eye on as it may revolutionize the way we configure our applications in the future!

--

--