3 ms·
One of the major reasons I dislike PHP is because of the include mechanism. When you include a file, you literally include the code of that file on that spot. Y
by gitaarik 12y ago
One of the major reasons I dislike PHP is because of the include mechanism. When you include a file, you literally include the code of that file on that spot. You can't assign it to a variable to namespace it or something. Also the file that get's included can use the variables of the file it gets included into. This allows for some crazy behavior that can be very hard to analyze.
So to keep an overview of what you're including or what actually gets included, you'll have to make rules like "the filename should be the classname lowercased" or "all views for this module are in this specific file" and "included files may never use variables of files they are included into".
A lot of PHP frameworks have these kind of conventions. That's good, people are less prone to create unclear program behavior, but, it's still possible if they want. And anyway, it's sad that you need these conventions to help you use the language in a sensible way.
That's my biggest annoyance of PHP anyway. Don't know how much people feel the same way?
- _lce0 12y agoAre you aware that you can include/require values? file a.php <?php return 42; file b.php <?php $a = include 'a.php'; var_dump($a); // (int) 42
- gitaarik 12y agoNo I didn't know that, that's pretty cool, why isn't everybody using that :P?
- marrs 12y agoThe autoloading basically fixes this, at least insomuch as you use the namespace that you want rather than require a file. Of course, you need an autoloader, and writing one is complicated enough that you'll want to use a pre-existing one, which means sticking to whatever conventions it uses for storing modules in the filesystem (probably a PSR), but realistically you're going to want to do this anyway. So yeah, it's more complicated than it needs to be but on the upside it's a lot more flexible too; you can pretty much define whatever naming convention you can think of.