Error Reporting in PHP

Many beginners to PHP are not aware of errors that may be occurring, seemingly, invisibly. This is at fault of the default error reporting setting, which excludes both notice-level and strict-level errors, and at the fault of the display_errors directive that may be turned off. To know of all the errors that are occurring in your script, ensure to put these following statements at the very beginning of your script.

ini_set('display_errors', 'on');
error_reporting(E_ALL | E_STRICT);

These can also be set in php.ini and is recommended that you do so for display_errors. If you have display_errors off in php.ini, you will not see errors outputted to the browser that prevent your script from executing, such as parse errors. If your script never executes, the call to ini_set() will never happen and display_errors will never be set to on. Therefore, for development, it is always recommended to have display_errors configured to on in php.ini. Calling error_reporting(E_ALL | E_STRICT) during your script is fine, however, so long as you do it at the beginning.

Now go make some error-free code!

Pass the NetBeans Please

A while back I posted about Geany. Now I do not even have it installed on my fresh 9.04 Ubuntu install. Why? I finally found an IDE that made sense! For whatever reason, in my hunt for a good IDE, my eyes went right passed NetBeans. Maybe I thought it was only for Java when I saw it. What a wrong thought that would have been too.

The thing about NetBeans is that it had that magical aspect no other IDE could present to me: intuitiveness. I opened it up, looked around a second, and just starting clicking around it like we've known each other for years. Okay, perhaps I didn't master it instantly, but I could do basic things like start a project the way I wanted to make a project, make a directory or file, and type code into it. That is a one up on some of my other IDE experiences.

When I had to start using SVN I found the SVN plugin equally easy and intuitive to use. I can view graphical file diffs, resolve collisions, and everything else through a brilliantly thought-out GUI. The only thing I have not liked is that I cannot work with the NetBeans main window when I have the commit window open. It would be helpful to see the diffs when I am typing my commit message to decide on what I should say. No big deal though, I have just started up Gedit to type my commit message instead and then did a simple copy and paste when the time came into the commit window.

Speaking of Gedit, when I just need to browse random source code, or type something quick, that is what I have been using. I do not consider it nearly as well featured as Geany, but then I do not consider Geany nearly as well featured as NetBeans. There is not any need for middle ground at this point.

If you have overlooked NetBeans and want to find an IDE that might work for you, go back and give it a shot. I wish I had found it earlier.

When Ordered Function Arguments Are Not Enough

Function arguments in PHP are not very flexible compared to a language like Python where you have something critically useful called keyword arguments. To make up for this, PHP programmers will setup their functions to take an associative array containing a map from argument name to argument value. This technique is used to avoid what would otherwise be a mess of function arguments without any natural order.

That setup is a tad clunky to use but it is the best we've got. To ease our troubles in dealing with the associative array as our argument list, the following code example may prove useful.

function foo(array $parameters = array()) {
    $defaults = array(
        'foo' => null,
        'bar' => 1,
        'zar' => array()
    );
    $parameters = array_merge(
        $defaults, array_intersect_key($parameters, $defaults)
    );
    extract($parameters);
}

This setup allows us to emulate argument functions. Each key seen in the $defaults array will be extracted to a variable, so $foo, $bar, and $zar are available. Extra keys provided in $parameters will not be extracted which is a safety precaution. However, keys in $parameters that match in $defaults will override the value found in $defaults with their associated values. Anything that $parameters does not override will end up with the value given in $defaults.

We have extracted a variable for each argument, emulated default arguments (to a degree, it is not exact of course), and didn't remove the ability to have a variable number of arguments either. The $zar variable, as an array, can take any number of values itself and thus could be treated as storage for variable arguments. To deal with arguments that we really need passed to the function, we can trigger an error or throw an exception if it does not change from its default value. Not bad.

Now, of course, if you only have three arguments I think you could get away with leaving them in the function declaration, even if they had no natural order. For more complex argument requirements, which does happen on occasion, this approach will help keep things comprehensible.

And I'm Off to the Races

The software development cycle is still something I have not fully experienced, not with other developers anyhow. I was very intrigued with Google Summer of Code 2009 but ended up disappointed when I found out I was just a couple months too young to participate. One of my favourite open source projects, Agavi, had applied to participate in GSoC 2009 but was not accepted. So, to make the best out of two unfortunate situations, I decided to join the Agavi team.

I am not working on the Agavi framework, though, sadly, but I am working on a project built on it named Redracer. What is this new awesomeness I speak of? It is a generic forge based on Agavi. A forge such as The Bakery or a more simplistic SourceForge. We may one day be as feature-filled as SourceForge, who knows.

As of now we are developing basic user management and getting our design together. Thus far I am very excited with being able to contribute and hopefully this is the starting line to a road ahead of many development opportunities. If you are a young programmer and are fumbling around with nothing much to do, join an open source project. The learning curve is a new challenge and the experience rocks.

Game Development

I have never stepped too far out of the web development genre of programming. However, game development has always been something that has intrigued me mainly because what you produce in the end is, hopefully, something actually fun. In contrast, writing a RPN calculator in C like I did a few weeks back does not produce the sort of repeating excitement a well polished game does.

Python has really popular game development libraries, and you have probably heard of them, such as Pygame and Pyglet. Then, from those, you have additional libraries to deal with certain types of games or just more toolsets for general purpose. For example, you can pick up Rabbyt for faster sprite management and it works for both Pygame and Pyglet.

I have experimented with all of those modules to some minor extent. I have also played with a couple frameworks, namely Opioid2D, for Pygame, and Coscos2D, for Pyglet. You do not get a link to Opioid2D because development on it has all but died and I wouldn't recommend starting any new project with it. Anyhow, it is all good stuff to experiment with if you want to see what making pong, Tetris, or asteroids feels like. I would always recommend using one of the available game frameworks because they really take the obscurities and make them more human.

So, as my adventure slowly progresses, I'll keep you posted on my findings. Game development is not my strength nor my real interest, but it is part of the diversity that keeps programming so darn interesting.


About

User