by Dominik Jungowski
24. November 2008
No Comment
Common situation: You have a formular on a webpage that has some radio buttons or checkboxes in it. Most likely there is some text besides the radio button / checkbox and most likely you will not be able to click the text in order to trigger the radio button / checkbox. This is rather annoying, especially since the solution to this is fairly easy:
by Dominik Jungowski
18. November 2008
Comments (4)
Sometimes you need to change servers quickly, for example if you're trying to find out which of your 18 webservers is having a problem. When modifying the hosts file, you would expect the browser to point to the new direction (at latest after restarting nscd).
When using Firefox you will have the problem that it will still not work. The reason is simple: Firefox caches DNS entries for 60 seconds by default. In case of 18 webservers that would mean that you have to restart Firefox 18 times or you will have to wait 18 minutes altogether.
For that reason I wrote a simple Firefox Plugin called DNS Cache, that allows you to deactivate the internal DNS caching of Firefox. You can download the plugin at https://addons.mozilla.org/de/firefox/addon/5914.
by Dominik Jungowski
15. November 2008
Comments (13)
Simple example: You want to open a file in PHP and there might be a slight possibility, that the file doesn't exist. Your code probably looks like this:
$fileHandle = fopen('text.txt', 'r');
if (!$fileHandle) {
print("Couldn't open file");
}
According to the php documentation, fopen will return FALSE if it can't open the file, but (and that's the annoying part), it will also generate a warning. Furthermore the php documentation suggests using @ before the function call to suppress the warning. Using @ before a function call is really, really bad coding and it makes your code slow (since @ changes the error_reporting to E_NONE before the function call and changes it back to what you had before after the function call) . But why return false, when it will generate a warning anyhow? Throwing an Exception would be the better way. Here's a way to do it.
First, we create a class containing a public static method that handles the error:
class ErrorHandler
{
/**
* catches php errors / warnings / notices and throws
* an exception instead
*
* @param int $errNo
* @param string $errStr
*/
public static function handle($errNo, $errStr=NULL)
{
switch ($errNo) {
case E_WARNING:
throw new RuntimeException($errStr,$errNo);
break;
default:
throw new Exception($errStr,$errNo);
break;
}
}
}
The method handle has the error number and the error message, which are both generated by php. Error number would be something like E_NOTICE, E_WARNING or E_STRICT. The error string in this case would be "failed to open stream: No such file or directory' in /var/www/phpinfo.php:15".
Now we add a public static function method to the same class, that overwrites the php error handler with the method we just wrote:
/**
* Overwrites the PHP error handler and uses our own
*
*/
public static function set()
{
set_error_handler(array(__CLASS__ , 'handle'));
}
Now there's only one thing left to do: We have to call the set method at the beginning of our script:
ErrorHandler::set();
If we now execute the script again, we will not get a warning, instead we'll get an uncaught exception: Fatal error: Uncaught exception 'RuntimeException' with message 'fopen(text.txt) [function.fopen]: failed to open stream: No such file or directory' in /var/www/phpinfo.php:15 Stack trace: #0 [internal function]: ErrorHandler::handle(2, 'fopen(text.txt)…', '/var/www/phpinfo…', 35, Array) #1 /var/www/phpinfo.php(35): fopen('text.txt', 'r') #2 {main} thrown in /var/www/phpinfo.php on line 15
The cool thing is, that now we can simply catch the exception and will not flood the phperror log with Warnings, where they are probably not needed. Our script would now look like this:
ErrorHandler::set();
try {
$fileHandle = fopen('text.txt', 'r');
} catch(Exception $e) {
print("Couldn't open file");
}
Note, that you can catch anything but a fatal error.
by Dominik Jungowski
14. November 2008
No Comment
If you're unhappy with your IDE (for whatever reason), you might want to try out Netbeans. The PHP support is rather new and not too stable yet, but apart from the download package being really small (the zip file containing both windows and linux binaries is only 30mb large - 90mb unpacked), it provides some really nice features/advantages:
- No more "Building workspace", indexing of projects takes seconds
- It's really fast
- It not only has code completion for PHP but also for CSS and JavaScript
- It shows browser compatibilities of JavaScript functions / methods / properties
- Very easy, good and fast renaming functionality (in the source code). Just type Ctrl+R, when you're on a variable or method and all the occurences in the file are renamed at the same time
- It's open source
Apart from that, it provides standard features like CVS / SVN support, defining projects and so on.
I have used Zend Studio for Eclipse so far (which is already quite good in my opinion), but I was really impressed how good Netbeans is when Petr Pisl demonstrated it to me at the International PHP Conference 2008. Since then, I'm only using Netbeans. According to Petr one of the next versions will also have Symfony support.
When using it, you have to remind yourself, that the php support is still far from being finished and that it still has some bugs. You can download Netbeans at http://php.netbeans.org - the latest version is 6.5 RC2.
by Dominik Jungowski
14. November 2008
No Comment
I wanna use this blog to write about developing with php (combined with mysql).
It definately won't be about "Today I programmed xy, which was a lot of fun", it will rather be about hacks, performance tweaks, strange bugs (that you should now about) or general stuff (like useful design patterns) for php and web development.
About me: I work as a developer at CHIP Xonio Online GmbH, where I am the release manager and the second scrum master of the price comparison (check http://www.chip.de/preisvergleich or http://www.xonio.com/preisvergleich)