PHP Pitfalls
creativepark.net
creativepark.net
Actually you should avoid using both mysql_pconnect and mysql_connect, because they're both functions from the deprecated old mysql library of database functions. The preferred way to deal with MySQL databases in 2012 is to use either PDO (http://php.net/manual/en/book.pdo.php) or the "improved MySQL" library (http://php.net/manual/en/book.mysqli.php), both of which are better architected and give you access to things like prepared statements that the old mysql library does not.
There's really no good reason to be using the old mysql library at this point. Unfortunately, however, there's a vast amount of ancient PHP tutorials, code snippets, and other copypasta floating around out there that uses it, so people who don't know better still pick it up.
PHP's libraries are full of Legacy Cruft that any other sane system would have deprecated and eliminated during a major revision transition.
The wholly inefficient and unsafe legacy MySQL lib is one of the primary examples.
Good on you for at least mentioning parameterized statements, but you didn't really follow through. You then go on to confuse the situation by talking about the old API.
Instead, you should have just said "Using this old API is a horrible idea. Don't. Ignore every guide or tutorial that does."
PHP has evolved considerably from its roots. The #1 problem with PHP is that it still lives in its parent's basement and hangs out with the same losers it met in Kindergarten.
Script kiddies everywhere will "benefit".
The syntax definition is used for some types of basic type-checking.
I'm completely serious.
Because of this, if you try to do a combination of property/array/etc. accesses that the grammar designers didn't envision, you'll get a very cryptic error. For example, this (which runs an anonymous function dereferenced from an array dereferencing from a static member of a class):
self::$views[$path]();
Doesn't work. It assumes that the result of self::$views[$path] is a string, and then complains there is no function with that name. (Yes, you can execute strings with names of functions, as if they were that function...) But this: $x = self::$views[$path];
$x();
Does work.Please, don't use PHP. It's horrible.
I wish I could upvote this comment a dozen times, for a combination of candor and useful fact.
The same applies to PHP. please do use it for a bit, learn about the crimes and don't repeat them :)
Deleted comment
Parse error: syntax error, unexpected '[' in D:\Projects\2012\45blocks\45blocks\src\framework.php on line 18
No it doesn't. $this->views[$path]();
should not throw an error. What version are you using? Fatal error: Using $this when not in object context in D:\Projects\2012\45blocks\45blocks\src\framework.php on line 19
It's a static class member.To the author: doesn't the fact that you have to point out all of these things to avoid say something to you about core issues with the language?
To you: doesn't the fact that you myopically dismissed the entire article due to a single paragraph say something about you? Why are you looking for a flamewar where one doesn't exist?
I get it -- you don't like PHP. That's your prerogative, and you're of course entitled to it, but this article isn't just another skirmishing ground for the flamewar that is "PHP SUCKS" vs. "NO IT DOESN'T". Rather, it looks to me to be more of a braindump from a PHP dev looking to enlighten new PHP devs with what he's learned.
Is that so patently offensive to you that you have to attempt and interject the played-out flamewar that is now a decade old?
Do we really expect the language elitists to actually read articles now? Just say something bad about PHP here and you'll get 20 instant upvotes. We're getting as bad as reddit here.
As someone who uses PHP, I found this article very enlightening.
I think PHP is to a large degree a victim of its own success. The runtime's ubiquity means that a lot of very incompetent people are fiddling around with things they don't understand. If you look at, say, PHP-related questions on Stack Overflow, you'll notice they are of extremely low quality that have nothing to do with PHP itself, but have everything to do with the person behind them not understanding the simplest things about programming.
I know I've fallen into many of the traditional PHP traps simply due to what I call language laziness. "Why do it this way when the other way is so much faster". PHP allows you to do quite a few things wrong very quickly.
I'm a closet PHP fan, but not compared to "better" languages like Python.
What? Can you give a single example?
The author is just giving some good advice to PHP programmers. If you don't like it, just don't read it.
There have been numerous responses already that debunk many of the claims presented there. Suffice it to say, you came to this with your mind already made, and nothing is going to change it.
So, why bother?
Yes, that's just as good as a real profiler.
I noticed there is some discontent with my use of the microtime() method. It's not intended to replace a serious debugger/profiler, but it is something that's available in any PHP environment for quick performance checks.
It's a pretty good attempt, though. I would have liked to see more links for more information, and you don't mention packagist at all.
Either way, a good start. Knowing about these ideas, and knowing why to care about them, is the first step to becoming a better programmer, regardless of the language.
It's true that I didn't include a lot of links to more advanced frameworks and libraries, mainly because I wanted to limit the scope of the article to the basics of the PHP runtime. I also think that in order to avoid mistakes people should think more about those basics, even when they happen inside convenient library methods.
If you think there are links to essentials that should be in there, you're welcome to contribute them in the comments.
But most of what he says is on my "List of Reasons to Never Use PHP Ever Again."