Req #81599 [Com]: adding the ability of modular import and export just like modern javascript

From: Date: Mon, 08 Nov 2021 17:07:05 +0000
Subject: Req #81599 [Com]: adding the ability of modular import and export just like modern javascript
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-237623@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=81599&edit=1

 ID:                 81599
 Comment by:         pooyaestakhry at gmail dot com
 Reported by:        pooyaestakhry at gmail dot com
 Summary:            adding the ability of modular import and export just
                     like modern javascript
 Status:             Feedback
 Type:               Feature/Change Request
 Package:            *General Issues
 Operating System:   All
 PHP Version:        Irrelevant
 Block user comment: N
 Private report:     N

 New Comment:

> > 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.


Previous Comments:
------------------------------------------------------------------------
[2021-11-08 17:03:43] 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 16:41:11] 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 16:28:19] 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 13:59:23] requinix@php.net

(don't mind the syntax error)

------------------------------------------------------------------------
[2021-11-08 13:59:00] 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?

------------------------------------------------------------------------


The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at

    https://bugs.php.net/bug.php?id=81599


--
Edit this bug report at https://bugs.php.net/bug.php?id=81599&edit=1


Thread (10 messages)

« previous php.bugs (#237623) next »