Export function in php

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

⚗️ Modules in PHP with the `import` and `export` syntax

License

nunomaduro/yorn

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Читайте также:  Python 3 sorted cmp

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

Yorn Preview

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:  export(function ($one, $two) < return $one + $two; >); # src/index.php:  $sum = import('math/sum'); echo $sum(1, 2);

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

# First, install: composer require nunomaduro/yorn 

Any function can be exported by using the export function:

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

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);

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);

Do you like this project? Support it by donating

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

About

⚗️ Modules in PHP with the `import` and `export` syntax

Источник

Export functions in PHP à la Javascript

Warning: This post is totally useless. It is the result of a fun private company thread.

Export functions in Javascript

In Javascript, a file can export functions like this:

And then we can import this function in another file like this:

import from 'foo'; console.log(times2(21)); // 42

Export functions in PHP

Every entity is public in PHP: Constant, function, class, interface, or trait. They can live in a namespace. So exporting functions in PHP is absolutely useless, but just for the fun, let’s keep going.

A PHP file can return an integer, a real, an array, an anonymous function, anything. Let’s try this:

What if our file returns more than one function? Let’s use an array (which has most hashmap properties):

 function (int $x): int < return $x * 2; >, 'answer' => function (): int < return 42; >];

To choose what to import, let’s use the list intrinsic. It has several forms: With or without key matching, long ( list(…) ) and short syntax ( […] ). Because we are modern, we will use the short syntax with key matching to selectively import functions:

 $mul] = require 'foo.php'; var_dump($mul(21)); // int(42)

Notice that times2 has been aliased to $mul . What a feature!

Is it useful? Absolutely not. Is it fun? For me it is.

Share

Like this:

One thought on “ Export functions in PHP à la Javascript ”

Totally get you. It makes absolutely no sense in PHP but playing with the language is fun. One of the reasons I love PHP over javascript is because of its built-in namespace system. It’s awesome. Javascript imports are just hacks. Like Like

Leave a Reply Cancel reply

Archives

  • October 2021 (1)
  • January 2021 (1)
  • May 2020 (1)
  • August 2019 (1)
  • May 2019 (1)
  • April 2019 (1)
  • March 2019 (1)
  • October 2018 (1)
  • September 2018 (1)
  • August 2018 (3)
  • February 2018 (1)
  • January 2018 (2)
  • November 2017 (3)
  • October 2017 (1)
  • August 2017 (1)
  • July 2017 (1)
  • June 2017 (6)
  • May 2017 (5)
  • April 2017 (4)
  • January 2017 (6)
  • December 2016 (2)
  • November 2016 (4)
  • October 2016 (3)
  • September 2016 (2)
  • April 2016 (2)
  • November 2015 (1)
  • October 2015 (1)
  • August 2015 (1)
  • July 2015 (1)
  • June 2015 (1)
  • February 2015 (1)
  • January 2015 (1)
  • December 2014 (1)
  • November 2014 (3)
  • September 2014 (5)
  • August 2013 (1)
  • March 2013 (1)

Subjects

Tags

skim, https://github.com/lotabout/skim.Skim is a fuzzy finder.> Skim provides a single executable: `sk`. Basically, anywhere you would want to use `grep`, try `sk` instead.#fuzzy #finder #RustLang

Why SQLite does not use Git, https://www.sqlite.org/draft/matrix/whynotgit.html.SQLite uses Fossil. The article doesn’t compare Git to Fossil, but instead explains why Fossil is better suited to them (and it brings nice improvement ideas for Git…).#sqlite #fossil #git #scm

There is decentralized MLS open implementation, https://gitlab.matrix.org/uhoreg/matrix-dmls.#mls #decentralized #encryption #matrix #RustLang

You can see MLS as TLS, but for client to client instead of server-or-client to server.RFC: https://www.rfc-editor.org/rfc/rfc9420IETF announcement: https://www.ietf.org/blog/mls-protocol-published/Support for MLS: https://www.ietf.org/blog/support-for-mls-2023/Google announcement: https://security.googleblog.com/2023/07/an-important-step-towards-secure-and.html

A giant leap forwards for encryption with MLS, https://matrix.org/blog/2023/07/a-giant-leap-with-mls/.The Messaging Layer Security (MLS) protocol is a new encryption standard defined by the IETF (RFC9420), to provide a way for users of a messaging service to communicate securely without servers being able to eavesdrop on their conversations.Matrix has been working on this with Cisco, Google, Mozilla, […]

Do you know how to create a Unity plugin?Coqui awesome Text-to-Speech project needs you, https://github.com/coqui-ai/TTS/issues/2589. Imagine being able to create any speech from a simple text, in multiple languages, with any voices (including voice cloning), based on open source technologies and state-of-the-art algorithms? You can make it real.#unity #tts #coqui #plugin #DotNet #python

Testing sudo-rs and improving sudo along the way, https://ferrous-systems.com/blog/testing-sudo-rs/.sudo-rs is a re-implementation of sudo in Rust. While testing sudo-rs, they have found several inconsistencies in the specification, and found 2 bugs in the original sudo implementation.This test suite can run on both implementations.Link to sudo-rs: https://github.com/memorysafety/sudo-rs.#sudo #posix #unix #test #safety #security #RustLang

What I’ve in mind now is to get the archive of my data, and publish them somewhere else. It’s a workaround, a hot fix, I don’t know what to think about this situation. Thoughts?

It’s been 5 years I’m on Mastodon. Since a couple of months, the new communities and people I meet are so much more valuable than what I could have on Twitter. Everything is better here with more people, please don’t mess with Mastodon. It’s not perfect, but it’s alright.On the other side, Twitter is a […]

Источник

Import and export only a function under namespace in PHP

The standard way of including files in modern PHP development is using namespaces and autoloading of classes using Composer.

So, for instance, if I have the autoload setup like following in my composer.json …

…And I have a file called HelloWorld.php under app/ which contains a HelloWorld class, we can define it using namespace like so.

 namespace App; class HelloWorld  > 

You can use/import this class in other files like so.

A function under a namespace

Recently, I was going through all the new features of Laravel 8 and I saw this queueable function which can be used to mark the closure based listeners as “queueable”. And this is the first time where I’ve seen only a function being defined under a namespace.

If we look closely at the source code of it, we get to know that the queueable function lies under the Illuminate\Events namespace like so.

//src/Illuminate/Events/functions.php  namespace Illuminate\Events; use Closure; if (! function_exists('Illuminate\Events\queueable'))  /** * Create a new queued Closure event listener. * * @param \Closure $closure * @return \Illuminate\Events\QueuedClosure */ function queueable(Closure $closure)  return new QueuedClosure($closure); > > 

As you can tell, the src/Illuminate/Events/functions.php only contains a function queueable under the namespace Illuminate\Events .

And because it’s under a namespace, you can import it elsewhere using the use function operator like so and start using it just like that.

use function Illuminate\Events\queueable; 

Usefulness

This is especially useful, in my opinion, in scenarios where you don’t want to create an entire class for a single function but still want to keep it under a namespace so that it’s easily importable like in the case of this queueable function.

PHP 8 in a Nutshell book cover

PHP 8 in a Nutshell book cover

Learn the fundamentals of PHP 8 (including 8.1 and 8.2), the latest version of PHP, and how to use it today with my new book PHP 8 in a Nutshell. It’s a no-fluff and easy-to-digest guide to the latest features and nitty-gritty details of PHP 8. So, if you’re looking for a quick and easy way to PHP 8, this is the book for you.

Caricature of Amit Merchant sketched by a friend

👋 Hi there! I’m Amit. I write articles about all things web development. If you like what I write and want me to continue doing the same, I would like you buy me some coffees. I’d highly appreciate that. Cheers!

Источник

Оцените статью