3 ms·
PHP needs modules. The way it solves code reuse by tooling that helps making class names longer to avoid collisions is just too cumbersome. Python: calc.p
by TekMol 2y ago
PHP needs modules. The way it solves code reuse by tooling that helps making class names longer to avoid collisions is just too cumbersome.
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.
- ulrischa 2y agoPHP has namespaces: https://www.php.net/manual/en/language.namespaces.php https://www.php.net/manual/en/language.namespaces.php But what is the difference writing abc\classname or abc_classname? Long classnames ist not a big problem
- xcjs 2y agoIt has Composer: https://getcomposer.org/ https://getcomposer.org/
- TekMol 2y agoComposer adds even more tooling around the fundamental problem that PHP does not have modules. 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.
- forgetfreeman 2y agoPHP not having modules is a non-issue. Proof: It's done fine for several decades without it. If you're running into namespace collisions in your codebase frequently enough to demand a language change to help get your shit together I don't even know what to tell you.
- phplovesong 2y agoThats why PHP code looks like Java. EVERYTHING is wrapped in a class, because of the shitty packaging PHP currently has. Namespaces are weak and can be inconsistent. Also the stdlib has no namespaces? Wtf?
- forgetfreeman 2y agoYou're pointing a finger at the wrong villain. Laravel's your culprit not core PHP. I have no idea what mind-altering substances that crowd got into when they decided to objectify everything all the time, but it isn't a hallmark of either the language or good coding practices and the result is so hard to debug it requires it's own toolchain. Fun Fact: Drupal's core team threw a big chunk of it's community developers overboard switching to this bullshit. Net result: they bricked the project's credibility listening to the wrong people and lost a bunch of market share and mindspace because of it. A shame too, the project could have credibly challenged Wordpress if they'd gone another way.
- tored 2y agoIt is not a non-issue. You can run into naming collisions by just using two different composer dependencies where each dependency depends on the same library but different major versions.
- shakesbeard 2y agoThat's just not true. It's not JS. Composer will install exactly one version of every package. If your dependencies have incompatible dependencies themselves, then Composer will refuse to install the package. And this fact has led to PHP packages actually being maintained and kept compatible with each other. Of course, sometimes you run into dependency issues, but it's worth it because you'll always have quite clean, flat and small vendor directory. Compared to the node_modules horror it's great for maintenance.
- phplovesong 2y agoWe dont need yet another adhoc dependency. All the PSRs out there are just a big ball of mud. We have incrementing numbers each years, and today there is something like PSR-12. Its a joke.
- foxpc 2y agoI cannot relate to this problem. The namespace is rarely a single "word". If you [follow PSR-4](https://www.php-fig.org/psr/psr-4/ https://www.php-fig.org/psr/psr-4/), you will create namespaces based on the directory structure - thus there would be no collisions possible.
- Dachande663 2y agoI think you’re being a bit disingenuous here. Almost all packages are vendor and package prefixed yes, but your own code from very easily be as simple as: 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.
- TekMol 2y agoThat is not valid PHP code. Try making a working example of it on an active codebin like this one: https://www.online-ide.com/ https://www.online-ide.com/ And link to it, then we can discuss it.
- Dachande663 2y agoIt is valid. I’m running 8.4 and with autoloading etc it all works as expected. Maybe the language you’re arguing against isn’t the same language anymore.
- phplovesong 2y agoThats does not work without a third party dependency (composer). You cant load functions like that in vanilla PHP.
- tored 2y agoIt becomes a problem if you want to use multiple versions of the same library, which may happen if you are using composer, e.g two different dependencies uses the same dependency but different major releases.
- mschuster91 2y agoIf you mean anything like the clusterfuck that is Java Modules, please keep that out of PHP, thanks. 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?
- juangacovas 2y agoSorry but your example shows that you know Python better than PHP.
- phplovesong 2y agoThis. PHP bolted on namespaces, back in 5.3 (iirc) and they failed to namespace the stdlib, and now 20 years later we are still stuck with the same garbage. PHP has only globals really, no module system what so ever.
- shakesbeard 2y ago> and now 20 years later we are still stuck with the same garbage. and it hasn't been an issue ever. because all userland code lives in its own namespaces.
- phplovesong 2y agoIt gets messy real fast. I have seen horrible long namespaces ala java all over the place. It only leads to bad design. Having no way to force things. PHP messed up pretty big when they made this ad-hoc namespace thing.