This is a good list for developers new to PHP. Most lists that target that group tend to have a lot of outdated, wrong information; optimizations that were important for PHP4 but irrelevant today.
This one seems to skirt that.
One change I would make is to use the very effective Filter Input library to check an email address before you check it's DNS record. It would prevent a costly DNS hit if the email address is just gibberish.
Edit: For the criticism below (and unexplicable down-mods), take note that I didn't say this is a good list for new developers. I said this is a good list for developers new to PHP
I disagree, I think this kind of list is another reason why new developers write poor PHP code. For example, as you highlight, there's no mention of checking to see if the string is an email address before taking the DNS hit.
Then number 1: what's the point of having a long string stored in the database if it's compressed? Either put it there so you can search with it or store it on the local filesystem.
5. Why on earth should we be encouraging people to write code like this?
6. The same point, and even the list's author doesn't know what's going on here. He's using variable variables, e.g.
As I say, new developers don't need these features and shouldn't be using them. For that matter, I don't think any developer should be using the features I mention above: am I wrong?
PHP's use of variable variable's is certainly easy to abuse, but in the right hands it creates a very elegant solution.
If we took away every tool that can be misapplied and misused, we wouldn't have very many left.
This is a crude example, but imagine, eg, loading up a data model. Maybe it is lazy loaded, maybe cached, whatever, but you have an array with keys that match properties.
You could use a giant switch, a slew of if(isset()) code, or you could do something like this:
public function __construct($data = array()) {
foreach($data as $k => $v)
$this->{$k} = $v;
}
Obviously, this is POC stuff, in reality you'd want to put a 1-line check in there to ensure the $k is a valid property in that class.
AFAIK, there's nothing you can do with variable variables that you can't do another way. But there are several things you can do much easier and more elegantly.
I get your argument. Programmers all over the place inherit sucky code from newbie developers. But there is no valid argument, IMO, to a language eliminating features because they can be misapplied.
The reason they write poor code is because they are beginners and beginners always make mistakes, use functions incorrectly, etc. In any language they code, not just PHP.
PHP is often more visible because it is more used and easier to write... But if PHP wasn't that popular, we would see the same issue with Perl, Python, etc.
I could have been clearer with what I meant there, and I agree with you: it's not a problem specific to PHP, and in some ways it's unfair to label beginners' mistakes as problems in the first place.
The point I wanted to emphasize is that by reading lists like these new users might think that this is a good way to write PHP, whereas I strongly disagree with this list existing in the first place.
There's also the slightly selfish point of view of not wanting to have to clean up code like this should I come across it in future ;)
I agree with you. But beginners need to learn from somewhere and most advanced programmers are busy at work and not writing articles :)
Plus, did you ever read a PHP book? Most of them give bad examples and bad code practices too... I just read one that said to disable all apache logging (including error logging) for performance reasons.
PHP encourages shitty code because it is already shitty code in a shitty language. It's the broken windows effect. All you need to know is from Rasmus himself
"There are people who actually like programming. I don't understand why they like programming."
I was a professional programmer in PHP for just under 10 years so I'm not just bandwagon jumping. I don't get paid by yahoo to post here so I'm free to be as sloppy as I like.
${‘a’ . ‘b’} = ‘c’;
echo $ab; // it will outputs c
I've seen people abuse that "feature" to write utterly unreadable messes of code that should have been covered in pasta sauce. Developers new to PHP should absolutely not be using this, not until they learn how to write clear, easy to read code.
This one seems to skirt that.
One change I would make is to use the very effective Filter Input library to check an email address before you check it's DNS record. It would prevent a costly DNS hit if the email address is just gibberish.
Edit: For the criticism below (and unexplicable down-mods), take note that I didn't say this is a good list for new developers. I said this is a good list for developers new to PHP