|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2021-11-08 13:09 UTC] pooyaestakhry at gmail dot com
Description:
------------
PHP is mostly known as a procedural/OOP language.adding the ability to import and export modules like functions, variables and constants would mean that doors of functional programming would open up to those us who love php but are not a fan of oop.
Test script:
---------------
import {Module,otherModule as other} from dirname(__dir__,1).'/strings.func.php'
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Oct 27 21:00:02 2025 UTC |
You can already do this. <?php // strings.func.php $Module = get_class(new class { public function whatami() { echo "This is Module\n"; } }); $otherModule = function() use ($Module) { return new $Module(); }; return [ "Module" => $Module, "otherModule" => $otherModule ]; ?> and <?php list["Module" => $Module, "otherModule" => $otherModule] = require "strings.func.php"; $otherModule()->whatami(); ?> But why would you want to, given that PHP is architecturally, technically, and philosophically completely different from Javascript?Ok, here is an example <?php // strings.func.php function makeItSafe($text) { /// assume some proccess is going on here. return $text; } function concat($t1, $t2) { return makeItSafe($t1) . makeItSafe($t2); } function addHappy($text) { return 'Happy ' . makeItSafe($text); } export {concat,addHappy} <?php //main.php import {concat as ct} from 'string.func.php'; echo(ct('Birthday','John')); <?php //sub.php import {addHappy} from 'string.func.php'; echo(addHappy('New Yeat')); echo is_callable('makeItSafe'); /// returns false in this way functions are easy to import when needed without the need to use include/require which in return pollutes global scope.So what you actually want is file-level scope? Let's say it reuses the "private" keyword: <?php // strings.func.php private function makeItSafe($text) { // ... } function addHappy($text) { return "Happy " . makeItSafe($text); } var_dump(function_exists("makeItSafe")); // true ?> <?php // numbers.func.php private function makeItSafe($number) { // ... } function addOne($number) { return makeItSafe($number) + 1; } var_dump(function_exists("makeItSafe")); // true <?php // sub.php require "strings.func.php"; require "numbers.func.php"; // no conflicts echo addHappy("New Year"); var_dump(function_exists("addHappy")); // true var_dump(function_exists("addOne")); // true var_dump(function_exists("makeItSafe")); // false ?> (note that if you literally want to avoid polluting the root namespace then you can use a namespace for the "hidden" functions) I'm going to go out on a limb here and say that the modules paradigm is not an appropriate feature for PHP, but if you disagree with that strongly enough then check out the RFC process. https://wiki.php.net/rfc