by TekMol · 1y ago · 0 comments
Python:
calc.py
-------
def sum(a, b):
return a+b
hello.py
--------
import calc
print(calc.sum(1, 2))
PHP: LongUniqueStringToHopefullyAvoidCollisions/Calc.php
---------------------------------------------------
<?php
namespace LongUniqueStringToHopefullyAvoidCollisions;
class Calc {
public static function sum($a, $b) {
return $a + $b;
}
}
hello.php
---------
<?php
require_once 'LongUniqueStringToHopefullyAvoidCollisions/Calc.php';
use LongUniqueStringToHopefullyAvoidCollisions\Calc;
echo Calc::sum(1, 2)
For people who don't know PHP it is probably hard to grasp and hard to believe: To avoid name collisions, you have to wrap your code in classes and then use the "namespace" and "use" keywords which function like prefixing the class names with a given string, so the class names hopefully do not collide with other class names in your codebase.And it does not do away with the problem that you have to preemptively wrap everything in classes and add a LongUniqueStringToHopefullyAvoidCollisions via the "namespace" and "use" keywords to all your classes to hopefully avoid name clashes in the future.
In any case - is there any concrete actual real world example in a modern Symfony/Laravel application where you run into issues while using a decent IDE?
PHP has only globals really, no module system what so ever.
and it hasn't been an issue ever. because all userland code lives in its own namespaces.
PHP messed up pretty big when they made this ad-hoc namespace thing.
namespace Me;
function test(){}
———
use function Me\test;
test();
Conflicting package names require you to be working on a very poorly written project (not using PSR-1) or a dependency that is even worse which is fewer and further between since composer.
Try making a working example of it on an active codebin like this one:
And link to it, then we can discuss it.