Modules in PHP with the `import` and `export` syntax.

dev-feat/first 2021-11-21 18:23 UTC

This package is auto-updated.

Last update: 2024-04-21 23:19:09 UTC


Yorn Preview

Build Status Total Downloads Latest Version License

Modules in PHP with the `import` and `export` syntax.

Yorn was carefully crafted to introduce the support for modules in PHP with the import and export syntax. It was created by Nuno Maduro.

Before the quick start, keep in mind that a module usually contains a collection of functions, and those functions are small units of independent, reusable code that is desired to be used as the building blocks in creating a PHP application.

As example, this is how a typical Yorn application would look like:

# src/math/sum.php:
<?php export(function ($one, $two) {
    return $one + $two;

# src/index.php:

$sum = import('math/sum');

echo $sum(1, 2);

🚀 Quick start

Remember, this is just an experiment. Don't use this in production.

# First, install:
composer require nunomaduro/yorn

✨ Exporting a function

Any function can be exported by using the export function:

# src/validators/zipCodeValidator.php:
<?php export(function (string $value) {
    return strlen($value) === 5;

✨ Importing a function

Importing is just about as easy as exporting from a module. Importing an exported declaration is done through using one of the import forms below:

# src/index.php

$zipCodeValidator = import('validators/zipCodeValidator');

echo $zipCodeValidator(8000);

✨ Default exports

Of course, you may want to import all functions in a module:

# src/index.php

$validators = import('validators'); // zipCodeValidator is imported also here

$zipCodeValidator = $validators->zipCodeValidator;

echo $zipCodeValidator(8000);

💖 Support the development

Do you like this project? Support it by donating

Yorn is open-sourced software licensed under the MIT license.