Comment

Comment by TekMol

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.
parent TekMol 1 year ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton