Wednesday, November 23, 2011

Nitty Gritty PHP

Well here's an interesting PHP lesson for you: there's an interesting trick to finding a count of the number of items in the second dimension of a two-dimensional array.  Say you have an array like:


$arr = array(
  "1"=>array(
      "red"=>1,
      "blue"=>2
  ),
  "2"=>array(
      "green"=>3,
      "yellow"=>4
  )
);

A totally contrived example, but count($arr) = 2 here, and we can use the recursive flag to arrive at count($arr, COUNT_RECURSIVE) = 6.

So the trick to figuring out how many second-level items I have is to just do count($arr, COUNT_RECURSIVE) - count($arr) = 6-2 = 4.

Of course, one could do something like

$count = 0;
foreach($arr as $key=>$value)
{
  foreach($value as $innerKey=>$innerValue)
  {
     $count++;
  }
}

And arrive at 4 as well.

So let's say you have an array where those colors point to stdClass objects.  All well and good, right?  Right - COUNT_RECURSIVE halts at the second dimension, and properly counts the objects.  Now let's say you have a multi (more-than-2)-dimensional array, such as:


$arr = array(
  "1"=>array(
      "red"=>array(1,2,3),
      "blue"=>array(4,5,6)
  ),
  "2"=>array(
      "green"=>array(1,2,3),
      "yellow"=>array(4,5,6)
  )
);

This is possible if you convert your stdClass objects into arrays when loading them into each color index. Of course, our nested foreach loops will still work, but what happens with COUNT_RECURSIVE?

Now, the count doesn't stop at the 2nd dimension - it recurses all the way down through the arrays.  Now you get count($arr, COUNT_RECURSIVE) - count($arr) = 18-2 = 16, a far cry from the 4 that we wanted.

Lesson learned.

Friday, June 24, 2011

Aspect Oriented PHP

I'd heard about aspect-oriented programming a couple years ago and always thought it was a great idea, but never had the time/motiviation to learn a whole new language and environment to do it.  Besides, aspect-oriented isn't mainstream enough (yet) to be well-supported.

Fast forward to working on a PHP application with plenty of cross-cutting concerns - logging, database management, etc., and a few google searches latter, I saw plenty of folks have tried to implement aspect-oriented PHP libraries on their own.  Some are compiled, some use other languages/frameworks to do the dirty work and provide syntactic sugar.  My favorite write up came from here.  Simple enough to understand, and simple enough to even get my own attempt working.  Here's the raw code in all its untested and unmodified first-draft form (and try http://codepad.org/PaL8PRce to see it actually work):





class LogAspect
{
public $object;
public $advice;
public function __construct($obj)
{
$this->object = $obj;
$this->advice = array();
}


public function weave($when, $method, $advice)
{
$this->advice[$method][$when][] = $advice;
}


public function __call($name, $args)
{
foreach($this->advice[$name]["BEFORE"] as $advice)
{
  call_user_func(array($this,$advice));
}
call_user_func(array(&$this->object,$name),$args);
foreach($this->advice[$name]["AFTER"] as $advice)
{
  call_user_func(array($this,$advice));
}
}


public function Log()
{
echo "Log it!\n";
}
public function LogAgain()
{
echo "Log it again!\n";
}
}


class Foo
{
public function __construct()
{


}
public function doA()
{
echo "DoA!\n";
}
public function doB()
{
echo "DoB!\n";
}
}


$foo = new Foo();
$foo->doA();
$foo->doB();


$fooAspected = new LogAspect($foo);
$fooAspected->weave("BEFORE","doA", "Log");
$fooAspected->weave("AFTER","doA","Log");
$fooAspected->doA();
?>


The output is:



1
2
3
4
5
DoA!
DoB!
Log it!
DoA!
Log it!


I was surprised that it worked as intended!  Now I'll have to do plenty to it to support multiple advices, etc.  But proof of concept!

Thursday, February 17, 2011

Back on the Google Reader horse

I'm back to my old tricks, after about a week-long moratorium on Google Reader.  It was brutal, but interesting to get a little perspective.  Now, I'm back to adding feeds to my gReader, BUT, I'm doing it a little differently.

For one, EVERY feed gets organized into a folder upon getting added.  For two, my folder structure is slightly more well defined.  I've got my always.comics folder, always.peopleiknow, always.peopleidontknow, and even sub-sub folders like always.pictures.cute, always.pictures.funny, etc.  Then there are the trial.programmers, trial.funny, to see if these guys are any good.  So far, so good.

What's scary is that in just a few days I'm already back up to 44 feeds, divided up into 14 folders.  The volume so far is manageable, at least, aside from Hacker News, which, at 725 posts per week (100/day, 4/hour, 1 every 15 minutes!!!) is a little bit insane.  What's worse is that a solid majority of the posts on HN are interesting and/or worth a read through.  Time sink, back in action.

Thursday, February 10, 2011

If you can dodge a wrench...

"If you can dodge a wrench, you can dodge a ball."
 - Patches O'Houlihan, Dodgeball

Today's quote is from dodgeball, which is on the brain after yesterday's stunning turn-around defeat.  While I wouldn't take credit for holding the team together, it was surprising that when I took a time-out for a conference call, my team was up 7-3, when I came back, they were up 7-6, and at the end of the night, we were down 9-7.  Talk about losing your mojo.  One game really stood out, where we had 5 players left, and the other team only had 1.  Through a series of insane dodges (by him), and missed catches (by us), he managed to get to a 2-to-1 man advantage and the team won the game.

While I don't have a distinct point to make, the quote is designed to inspire some cohesion in these ramblings.  I sort of take it to mean if you can dodge the small, hard, focused, precisely-aimed challenges that come your way in life, you can dodge the big looming ones too.  By practicing dodging the day-to-day, or at least, rolling with the punches, you'll be ready to handle the big challenges that come up.

"Ok, you want me up in a cage, then I'll come out in beast mode
I got this world stuck in the safe, combination is the G-code
It's Weezy motherfucker, blood gang and I'm in bleed mode"
- L'il Wayne, No Love

Monday, February 7, 2011

Google Reader Bankruptcy

Well, I finally did it.  After struggling mightily, I decided to declare Google Reader bankruptcy.  Last night, I completely unsubscribed from all my RSS feeds.  I did take the time to export them, however, so that I'll be able to recover a few of the more important ones if/when I ever feel like it.  Call it a soft bankruptcy.

It feels a little odd to not automatically pop open Greader all the time, or to pop it open and realize I don't have anything in there.  On the other hand, it has saved me minutes (hours?) of productivity today.

I'm also trying not to reflexively add RSS feeds back in just because I find one article of interest on a blog.  I really need to re-establish some ground rules about what gets in and what doesn't.

Oh well, here's to reducing a time-suck for 2011!