32 points · by willmacdonald · 13y ago · 54 comments
Not this again! I've been working with PHP for over 10 years and seen @ suppression used at companies in the most ridiculous ways imaginable. The most common, and I believe incorrect, way @ suppression is used is when using fopen. Why do people do it so often? Probably because it says you can do so right in the php.net manual http://us2.php.net/manual/en/function.fopen.php and has so for years. I've really lost track of how many bugs I've come across over the years in production code that could be traced back to someone @ suppressing fopen warnings. I don't care if you read some blog where the author claims using @ suppression will give you a magical 1000% increase in performance. JUST DON'T DO IT.
Oh wow, that's impressively awful, way more evil than the other common "just turn off notices in php.ini" hack.
Yet another reason not to use smarty I guess.
added: Apparently someone on php.net agrees with you
http://php.net/manual/en/language.operators.errorcontrol.php...
they say it takes 1.7 times as long BUT that is only .005 ms in their example
The more deeply nested the callstack is when you supress, the bigger the performance hit (slight generalisation and slightly from memory).
Someone posted here a clear example of why (in earlier php5) it generates far worse opcode:
http://derickrethans.nl/five-reasons-why-the-shutop-operator...
EDIT: I am wrong. See below.
--EDIT
I hadn't even got as far as the disk i/o section, after reading no words can do justice to the complete lack of understanding shown here with regards to filesystems, caching, SSD's and linux.
Very much agree with you.
When I was first starting out programming commercially, one of the best bits of advice I received was something along the lines of "Take a step back and think about why you're using these crazy constructs - chances are there's a much simpler way of doing things."
The lesson being: if you're having to write code like this, chances are it's because you're creating a mess. Take a step back and work out a simpler solution.
There are better ways to do PHP... and there is bad PHP code examples and also good PHP code examples like there is in any language.
As a contrived example, an article about "How I replaced my engine using a crowbar" might lead people to believe that is the ideal way to replace engines and have mechanics groaning everywhere. If however the article was "How I replaced my engine using a crowbar whilst stuck in the desert with no other tools to hand" then everyone would understand a) don't do this in normal circumstance, b) there is another way to do it in less normal circumstances.
Replace "PHP" with "Jewish" and would you find that comment of yours acceptable?
Edit: I can only assume that the down voters find discrimination acceptable behavior in this community.
OK I get that PHP doesn't support atomic operations, but there are so many different errors that could happen when working with files. Say a permissions error, if the permissions of the file were changed between it being created and deleted that's an issue that needs resolving before your tmp folder fills up, it's easy enough to fix and should be reported as an error (be it to the front-end or to a backend logging service).
Surely there is a better way to handle these race conditions without just ignoring all filesystem related errors.
The best/real solution is probably implementing flock so that it can issue real filesystem locks, rather than relying on a file resource.
We're using smarty at work now, and while we probably won't drop it in this project (too much working code written in it), we might learn something for the future.
I believe Twig is a fast and secure PHP template language.
Really, PHP itself is a fine templating language if used properly. Get to know the short tags (including the short echo <?=$stuff?>) and the alternative syntax for conditional statements and you are good to go.
I think most of the work here will happen in memory.
Surely file_exists() is simply an operation on the disk index where the relevant block will be sitting in RAM from the first point that the operation is done. Subsequent operations on the same data shouldn't involve hitting the disk again until an actual write has occurred. Provided you have sufficient RAM this sort of thing is what kernel page cache is designed for.
Not to mention that thinking about a frequently used performance critical piece of PHP code doing large amounts of @fopen() and @unlink() fills me with dread.
PHP warnings are a debugging tool, not a substitute for exceptions. Suppressing them is not the equivalent of a catching and ignoring an exception.
Putting locks everywhere in a futile attempt to prevent the filesystem from changing under you is futile. It's fundamentally an external i/o device out of your program's control. It is never safe or reasonable to infer from the result of one filesystem operation that a subsequent one will not fail.
process switching =/= parallel execution multi-threading =/= multi-tasking
Only certain single core processors can execute in parallel, so to say that it's possible to execute in parallel on a single core without specifying that not ALL single core CPUs can do it is bad form.