php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #81599 adding the ability of modular import and export just like modern javascript
Submitted: 2021-11-08 13:09 UTC Modified: 2021-11-21 04:22 UTC
Votes:2
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: pooyaestakhry at gmail dot com Assigned:
Status: No Feedback Package: *General Issues
PHP Version: Irrelevant OS: All
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: pooyaestakhry at gmail dot com
New email:
PHP Version: OS:

 

 [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'


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-11-08 13:59 UTC] requinix@php.net
-Status: Open +Status: Feedback
 [2021-11-08 13:59 UTC] requinix@php.net
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?
 [2021-11-08 13:59 UTC] requinix@php.net
(don't mind the syntax error)
 [2021-11-08 16:28 UTC] pooyaestakhry at gmail dot com
Hi requinix, your solution while it works but still uses classes under the hood and more importantly, it's complicated, which goes against one of php's main strengths, it's simplicity.

i don't necessarily want php to look like javascript. what i like to see is to have a more functional programming friendly php.
 [2021-11-08 16:41 UTC] requinix@php.net
> but still uses classes under the hood
So? If you don't want to use a class then don't use a class. But what else was "Module" supposed to be in your example?

> more importantly, it's complicated,
That's right. But it's how Javascript modules work. You can't pick and choose which parts of modules you keep and which you don't because everything is interconnected.

What would your version of strings.func.php look like?

> i don't necessarily want php to look like javascript.
Me neither. But if you say "I want modules like Javascript" then you're going to have something that looks like Javascript.

> what i like to see is to have a more functional programming friendly php.
"Modules" are a separate concept from functional programming.

PHP is not designed to be a functional language but you can still hammer in that screw using closures and lambdas.

How about a more concrete example of what you want to be able to do with PHP but currently are not?
 [2021-11-08 17:03 UTC] pooyaestakhry at gmail dot com
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.
 [2021-11-08 17:07 UTC] pooyaestakhry at gmail dot com
> > i don't necessarily want php to look like javascript.
> Me neither. But if you say "I want modules like Javascript" then you're going to have something that looks like Javascript.

Having similarities with javascript is neither a good, nor a bad thing.
but implementing new features that can help using php with different programming paradigms is good.
 [2021-11-08 17:33 UTC] requinix@php.net
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
 [2021-11-08 17:36 UTC] pooyaestakhry at gmail dot com
Thx for your responses, i will consider it.
 [2021-11-21 04:22 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.
 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Sat Jan 22 00:03:34 2022 UTC