January 21, 2008

Vector for php, surely not?

Ok before you try to hit me let me explain. This is not just a boring Object Orientated wrapper for storing PHP object in an array. It’s a multi-indexed, self sufficient class.

Too many times I’ve seen my code swell with foreach loops, looking for elements with a certain property, or to call a function of each object in an array. Additionally making sure the keys of an array match one particular property and stay in sink with them. So to try and side step all these I’m started Vector, I’m sure others have done similar, but reinvention is one of the fun things about being a programmer (and one of the few things which make it similar to being an artist).

So here’s how you use Vector currently:



//initialize a Vector and add a index that uses the username field of each object entered into the array
$users=new Vector();

//add two objects of class User into the vector
$users->addObject($frank=new User('frank.n.stein','monster'));
$users->addObject($vlad=new User('count.dracula','vampire'));

//call the checkpassword method of the object whose username is count.dracula
if($users->exists('username','count.dracula') && $users->username['count.dracula']->checkPassword('vampire'))
echo 'password correct!';
echo 'password false';

I hope to include more features later. But it’s a start.