This might be of use to someone

I’ve been writing PHP for a long time now. And one of the things that I see quite often is that people just dont get multidimensional arrays, and how to effectively store data in them for looping. Allow me to lead by example.

  • $store[‘fruit’][‘apple’]=3;
  • $store[‘fruit’][‘orange’]=1;
  • $store[‘berries’][‘blueberries’]=100;
  • $store[‘berries’][‘strawberries’]=100;
  • $store[‘fruit’][‘mango’]=5;
  • foreach ( $store as $type => $val ) {
    • foreach ( $val as $name => $qty ) {
      • echo “We have {$qty} {$name} ({$type} are good for you)rn”;
    • }
  • }

of course where the array comes from and what you do with it are completely optional. But it’s important to know that the array indexes should be as informational as the array values. Otherwise you arent using arrays properly.

Generic function/class names

This is especially true when you’re writing a library which is meant to be absorbed into a larger codebase, but also true of a codebase in which you know you will be using foreign libraries to accomplish tasks. And I’m as guilty as anybody when it comes to this!
When you are naming your functions and classes be mindfull of the possibility of collision. I think that we (as a group of programmers) are generally mindful of this when we lay out things like our database schema, but can overlook this when we’re writing our libraries.
For example a class/function name of something like “database” seems good ad first glance: It’s clear, to the point, and descriptive (okay… *somewhat* descriptive). However consider that anyone who’se writing anything even remotely related to a database (and these days what *isnt* tied to a database?) will think to themselves at one point “hmm if i name the class database, it’ll be short enough not to be annoying to type, long enough to describe what its used for, and no one’s going to think its used for processing text strings!”

So, now, its possible that every library has a right to use this class name for their code because of its qualifications. But then you will be limited to only using one persons external libs (assuming that your internal libs arent already using it)

What we *ought* to do is, for our project Foo, call the class “databaseFoo”, then we can simply use something like $database = new databaseFoo; and we loose basically nothing, and be assured of compatibility with other libs.

DK

Playing with loopholes

The most amusing things happen when you’re been coding a long time. I found, and used a “hole” in one of our database routines (which has since been fixed)

getOneValueFromTable($srcTable, $srcField, $whereField, $whereValue);
produces SQL like “SELECT $srcField FROM $srcTable WHERE $whereField = ‘$whereValue'”. Can you spot the potential problems in something like this? Consider this: getOneValkueFromTable($srcTable, $srcField, ‘1’, ‘1’; delete from $srcTable; ”);

Granted its not much of a problem if only proper developers are able to use this function, but if any untrusted party were able to affect any of those variables… big problems…

Everyone should know this. If you didnt know this kind of a problem existed… consider yourself warned. This has been a public service announcement. We now return you to the normal silence found on this blog

😉

cheers
DK

PHP (PHP5) Interface to Amazon’s S3 Service (Version 0.1)

Something I threw together in about 6 hours… it’s a good start (and its workable) but needs a lot of work (polish, etc)… but right now there is literally nothing out there even remotely close to this class. So I thought I would share it with the world… Perhaps I’ll setup a project page and put it under subversion if people care to actually contribute.

http://blog.apokalyptik.com/storage3.phps

Update: I’ve put together a “release” of this project. Please visit The Storage3 Interface to Amazons S3 “Simple Storage Service” home page.

Of Xcode and SVN

As i mentioned (now) 2 posts ago I got subversion and Xcode working together. Problem is that apparently any time someone adds a file to subversion Xcode will first properly update the working codebase, but then fail to add the new file to the subversion project. Long story short this makes working with xcode on a collaborative SVN controlled project a *real* pita… More on this later If i find anything out.

As a side note Scott apparently liked the Textmate [Editor|IDE?] which apparently has svn support I’m assuming he’ll iether give it high praise or grind it beneath his proverbial boots once he’s given it a shot…