Skip to content
⚗️ Modules in PHP with the `import` and `export` syntax
Branch: feat/first
Clone or download
Latest commit caedd3b Sep 2, 2019
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github wip Aug 31, 2019
docs wip Aug 31, 2019
src wip Aug 31, 2019
tests wip Aug 31, 2019
.editorconfig wip Aug 31, 2019
.gitattributes wip Aug 31, 2019
.gitignore wip Aug 31, 2019
.travis.yml wip Aug 31, 2019 wip Aug 31, 2019 wip Aug 31, 2019 wip Aug 31, 2019 wip Sep 2, 2019
composer.json wip Aug 31, 2019
phpstan.neon.dist wip Aug 31, 2019
phpunit.xml.dist wip Aug 31, 2019

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.

You can’t perform that action at this time.