Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Not sure what you mean about global variables. PHP 5 had the same scoping rules like it has now. You couldn't access global variables directly from anywhere but the toplevel execution context.


Right. So if file a.php has:

    <?php $MY_SOCKET_TIMEOUT = 5;
And file b.php has:

    <?php require(‘a.php’); $MY_SOCKET_TIMEOUT = 10;
That’s a problem. Same with every function and class name. Oh and requiring/including a file is not idempotent because it’s just string concatenation because they are not modules. None of that should be a part of a modern language. If they haven’t fixed that in PHP 8, then it’s still broken.


So... don't do that? You're correct, it's possible to write blisteringly awful code in PHP. Ditto every other programming language ever.


There is an argument for judging programming languages based on the worst code possible within the language, due to the fact that with a wide enough ecosystem, you will regularly encounter lots of stuff in the median between that and your “just don’t do that” standard.

This is one of the reasons I love Go: the worst code possible in it is still pretty readable.

The worst code possible in PHP is scattered across thousands of files corresponding to individual routes, and isn’t indented at all.


But PHP makes it way too easy and provides few ways to guard against it. Well technically now it does with namespaces but that’s still less than ideal. And my point is that they are just now starting to catch up to more sane languages.


require_once has existed since PHP5 at least (when I started using it).


That’s a hack, not a solution. You still can’t import just class A out of A, B, and C that are all defined in the same file. And even if you require_once file x.php, you don’t have any guarantees that that file won’t require/include file y.php that you also intend to include. PHP files are treated as files, not modules and that’s fundamentally broken.


I guess you like modules. Someone else may like string inclusion, because it allows for some other forms of splitting the code that modules would not allow and fulfills the primary purpose of a PHP script, which is templating. Neither is fundamentally broken. You're just focusing on one aspect (organizing code), while ignoring the other.


Ah and there it is. PHP is fundamentally a templating system with a programming language built-in. Imagine using Handlebars.js to write your business logic.

I do think code organization is one of the primary jobs of a programming language/ecosystem. I want my tools to help me be better about that, not worse. Oh and given that most projects use a bunch of library code, I would very much prefer a system that has a consistent code organization pattern so I don’t have to guess how it all works. Take a look at the source code for something like WordPress or WooCommerce. These are mature projects. And yet it’s hard to shake the feeling that it’s all spaghetti inclusions.

But also there are many other issues with PHP that I mentioned in a prior comment. Here, if you’d like for a better read on the subject than I can produce: https://eev.ee/blog/2012/04/09/php-a-fractal-of-bad-design/


> I do think code organization is one of the primary jobs of a programming language/ecosystem. I want my tools to help me be better about that, not worse.

Isn’t splitting up all your classes into separate files considered to be a good thing?

Considering how huge we can make files in JS I’m sort of inclined to consider that a problem instead of a feature.

I think any project that started around PHP 4/5 will look like Wordpress. Looking at more recent projects (after PSR’s became a thing) would be more representative.


It is but when you use third party code, what guarantees do you have that it didn’t do something stupid like declare a variable like $TIMEOUT? Whether on purpose or by accident. And of course any class or function become global even if you run include/require inside the body of a function or a method.


Include inside the body of an anonymous function if you care about toplevel $TIMEOUT=123 overriding some global variable.

Anyway you don't have any guarantees in JS either, for example. Imported module can just redefine Date object prototype or any global via globalThis. If you don't trust the libraries to not make a mess, don't use them is the only real solution. And it will be completely silent as opposed to PHP failing loudly when you try to redefine constant or class or function.


Variables would stay inside the scope of your anonymous function. Functions and classes will become defined globally. Surprise!


Ah, I don’t think so. It’s a different way of thinking about it, but it’s hardly broken.

It’s only broken if you expect files to be modules and they’re not.

It’s just as (if not more) confusing if you go from PHP to JS and expect modules to work like files.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: