[php-src] Issue #15455: Modules (Import & Export Variables and Functions)
| From: | trymeouteh | Date: | Sat, 17 Aug 2024 02:43:57 +0000 |
| Subject: | [php-src] Issue #15455: Modules (Import & Export Variables and Functions) | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-247517@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/15455
Author: trymeouteh
### Description
JavaScript allow for importing and exporting modules for bundling several scripts into one file.
This is not needed for PHP since it is server side language but the JavaScript import and export
module feature is also useful for organizing source code into smaller files.
Lets say you want to create or organize your PHP package like this...
```
- mypackage
- src/
- a.php
- b.php
- c.php
- composer.json
- index.php
- README.md
```
```
<?php
// index.php
class myPackage {
public $a import 'src/a.php';
public $b import 'src/b.php';
public $c import 'src/c.php';
}
```
```
// composer.json
{
"name": "johndoe/my-package",
"type": "library",
"version": "1.0.0",
"license": "MIT",
"autoload": {
"files": [
"index.php"
]
}
}
```
```
<?php
// src/a.php
export function() {
echo 'My Method A';
}
```
```
<?php
// src/b.php
export function() {
echo 'My Method B';
}
```
```
<?php
// src/c.php
export function() {
echo 'My Method C';
}
```
And to simply use the methods in the package...
```
<?php
require 'vendor/autoload.php';
$myVariable = new myPackage();
$myVariable->a();
$myVariable->b();
$myVariable->c();
```
A way to organize your code and be able to merge your functions into a single class easily.