Skip to content
⚗️ Modules in PHP with the `import` and `export` syntax
PHP
Branch: feat/first
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github
docs
src
tests
.editorconfig
.gitattributes
.gitignore
.travis.yml
CHANGELOG.md
CONTRIBUTING.md
LICENSE.md
README.md
composer.json
phpstan.neon.dist
phpunit.xml.dist

README.md

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:
<?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
<?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
<?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.