nunomaduro / yorn
Modules in PHP with the `import` and `export` syntax.
Fund package maintenance!
nunomaduro
Patreon
www.paypal.com/paypalme/enunomaduro
Requires
- php: ^7.2
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.15
- localheinz/phpstan-rules: ^0.10.0
- nunomaduro/pest: dev-master
- nunomaduro/phpinsights: ^1.7
- phpstan/phpstan: ^0.11.12
- phpstan/phpstan-strict-rules: ^0.11.1
- roave/no-floaters: ^1.1
- symfony/var-dumper: ^4.3.3
- thecodingmachine/phpstan-strict-rules: ^0.11.2
This package is auto-updated.
Last update: 2024-12-22 00:44:36 UTC
README
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.