Sunday, May 24, 2009

The new (not really) PHP.

Like everyone else, I learned basic PHP on my own in high school. It was a big fad back then, filling the role that Ruby on Rails fills now as the "cool" web development platform. It was also about as practical as Ruby on Rails is today (yeah Ruby on Rails is great for prototyping stuff, but it doesn't perform so hot and "convention over configuration" pretty much just means "I'm afraid I can't let you do that, Dave").

But after about 5-6 years, I finally took another look at PHP, and I'm really starting to like it. With the newer versions of PHP, you get OOP. That's right, you can make classes and objects in PHP! It also has a really nice set of Reflection classes that let you create and manipulate objects of unknown type dynamically.

I spent some time setting up a personal codebase for PHP-based websites, and I wanted to share a snippet from it. I'll probably post more later on (enough to make it reusable), but I'm really proud of this function. It's in a Command class that I wrote which I'm using for database interactions. Essentially, you give it a class name($className), some sql ($commandText), sql parameters ($parameters array of Parameter($type, $value)) and it'll convert each resulting row from the sql into an object of the given class. The only stipulation is you have to define a constructor in the class that takes in every property of the class. I might add a version that takes an Interface name and uses that to determine which properties to set.

The first half of the function binds a dynamic number of parameters to the prepared SQL statement.
The second half forms the objects form the result.

Enjoy!


public function fetchArray($className)
{
$this->ensureConnection();

$stmt = $this->connection->mysqli->prepare($this->commandText);

if(!$stmt) die("Statement could not be prepared.");
$paramVals = array();
$typesArr = array("");
foreach ($this->parameters as $parameter)
{
$typesArr[0] .= $parameter->type;
$paramVals[] = &$parameter->value;
}
call_user_func_array(
array(&$stmt, "bind_param"),
array_merge($typesArr, $paramVals));
if(!$stmt->execute())
die("Statement execution failed for:\n " .
$this->commandText . "\n\n with parameters(" .
implode(", ", $paramVals) . ")");

$reflectClass = new ReflectionClass($className);
$reflectProps = $reflectClass->getProperties();

//Create an array of variables.
//Then create an array of references to them (to pass to bind_result)
$propValueHolders = array();
$propRefHolders = array();
for($i = 0; $i < count($reflectProps); $i++)
{
$propValueHolders[] = 0;
$propRefHolders[] = &$propValueHolders[$i];
}

call_user_func_array(array(&$stmt, "bind_result"), $propRefHolders);
$ret = array();
while($stmt->fetch())
{
$ret[] = call_user_func_array(
array(&$reflectClass, 'newInstance'),
$propRefHolders);
}

$stmt->close();
return $ret;
}

Tuesday, April 14, 2009

Chicken feet are Phoenix Feet

You can order chicken feet from a large number of authentic Chinese restaurants. Only they're not called chicken feet. The dish, in Chinese, is called Phoenix Feet. It's such a ridiculous exaggeration, but it sure makes them sound more exciting, right?

It's been a while since my last attempt, but I'm now working in New York City for a company in the financial services industry. Last week, I was one of a few developers from my company selected to attend a 3-day Wintellect conference in midtown called Devscovery that started today. The keynote speaker was Scott Hanselman, who gave a talk called "How Social Networks Can Make You a Better Developer." I'm not sure he stayed on topic very well (as he barely touched on social networks at all), but it was a great talk. His real focus seemed to be that every professional developer should have a blog.

It makes sense careerwise. It'd be great to go to job interviews and be able to say "You can check out some of the more interesting tech problems I've handled by going to http://hitsthings.blogspot.com." Or to make connections to people in the industry through your blog posts. Or just have a record of things you've done to look back over 5 years down the road.

So I'm going to give that a try. My hope is that this blog will become a great contribution to society and to myself and I'll do my best to make sure it is. But at worst, this blog will be a record of interesting tech issues I encounter.

For certain, this blog is rising from the ashes of last year. Whether it's a phoenix or just a chicken will soon be seen, but it'll be exciting for me either way.

Wednesday, January 16, 2008

I'm still getting used to my glasses again, and usually I don't notice the frames, but when I'm outside in the light, my vision becomes very clear and the frames become very bright. I don't think it's a physical phenomenon, I think it's a perception issue. Like how you usually ignore the fact that your field of view jostles as you walk, but if you focus on it, it's quite obvious. Or how there is almost always some kind of ringing in your ear, but you never notice it unless you listen for it explicitly. I was thinking about how cool it might be to have a film that included on the little perceptual weirdnesses like that that no one really thinks about. First-person perspective scenes would obviously need to be included to be able to capture things like that.


My video rentals from clemons are late. I have to return them and my books. I'll do it during the dedicated hour tonight.

My glasses are smudged, I need to wipe them tonight.

I need to email my old STS professor about getting my proposal so I can do the progress memo for norton.

course action, degree audit, get proposal tomorrow.
I'm going to start using this as a sort of personal diary. I was listening to a pharmacology lecture podcast, and the professor talked about how much easier it is to get something done when you give meaning and ritual to it. I decided that I want to encourage myself to spend some time every night organizing my thoughts and my life, and keeping the info in a separate blog, and writing in it at midnight every night would be the first step toward a formal ritual.

So every night, I will post in this blog about ideas I've had during the day, and things I'll be wanting to do the next day. After I finish posting, I'll dedicate an hour to getting those things done, and then go to bed.

First post coming up soon....

Tuesday, July 31, 2007

First Post!

Just thought it'd be nice to have this URL saved. I'll use it though eventually.