"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How can I achieve Immediately Invoked Function Expressions (IIFE) in PHP?

How can I achieve Immediately Invoked Function Expressions (IIFE) in PHP?

Published on 2024-11-08
Browse:850

How can I achieve Immediately Invoked Function Expressions (IIFE) in PHP?

PHP's Immediate Function Invocation

In PHP, the Immediately Invoked Function Expression (IIFE) has a partial equivalence in PHP 7, as you can invoke a function immediately after its definition. However, PHP 5.x doesn't support this feature.

For PHP 7, an example would be:

(function() { echo "yes, this works in PHP 7.\n"; })();

For PHP 5.x, the closest approximation is:

call_user_func(function() { echo "this works too\n"; });

This usage allows you to execute code immediately without the need for global variables and functions, making it convenient for encapsulation and dependency injection.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3