My blog has moved to http://blog.derekbobo.com

I moved my blog. Visit http://blog.derekbobo.com for the new one!

Archive for February, 2009

Recursively autoload classes in PHP

Posted by derekbobo on February 17, 2009

For those of you who are not aware of PHP’s autoload function, you can read about it here. Basically it allows you to do some lazy loading of classes. Instead of having to manually include files, you can just instantiate classes on the fly. If the object name is not in scope it will call the autoload function to attempt to load it.

For example:

include_once “common/classes/myclass.php”;
$objMyClass = new MyClass();

With an autoload function defined this can now be coded by simply doing this:

$objMyClass = new MyClass();

This is particularly helpful with large projects as it helps keep your code lean. You can read the basic examples from the manual, but I wanted to share a way to recursively autoload files. Here is my Autoload.php file which simply needs to be included by any page you wish to leverage autoloading on. In my framework I only need to include it once, basically in my index.php file.

  • The constants are first defined in a Config.php file.
  • All files in the HELPER_DIR are included as these are general functions that are leveraged in classes or inline.
  • After including helper files this would essentially start at the next defined root directory and then look through all files and folders while attempting to include the appropriate class. (LIBRARY_DIR then ADDON_DIR then CONTROLLER_DIR then MODEL_DIR then VIEW_DIR then EXTENSION DIR).
  • File names should be the same as the object name, minus the .php extension. For example if your class name was MyClass it should live in a file called MyClass.php.

I’m not suggesting that this is an optimal solution. In fact, the way I’m determing if the include file was found is a bit of a hack, but it does work and demonstrates a handy function of PHP. If there are any questions on this implementation don’t hesitate to ask.

<?php

doFindAndInclude(HELPER_DIR, "", true);

function __autoload($strClass)
{
   $blnDone = false;
   if( !$blnDone )
   {
      $blnDone = doFindAndInclude(LIBRARY_DIR, $strClass, false);
   }

   if( !$blnDone )
   {
      $blnDone = doFindAndInclude(ADDON_DIR, $strClass, false);
   }

   if( !$blnDone )
   {
      $blnDone = doFindAndInclude(CONTROLLER_DIR, $strClass, false);
   }

   if( !$blnDone )
   {
      $blnDone = doFindAndInclude(MODEL_DIR, $strClass, false);
   }

   if( !$blnDone )
   {
      $blnDone = doFindAndInclude(VIEW_DIR, $strClass, false);
   }

   if( !$blnDone )
   {
      $blnDone = doFindAndInclude(EXTENSION_DIR, $strClass, false);
   }
}

function doFindAndInclude($strDirectory, $strFilename, $blnIncludeAll)
{
   ob_start();
   doRecursiveFindAndInclude($strDirectory, $strFilename, $blnIncludeAll);
   $included = ob_get_contents();
   ob_end_clean();
   return $included != "";
}

function doRecursiveFindAndInclude($strDirectory, $strFilename, $blnIncludeAll)
{
   if( $handle = opendir($strDirectory) )
   {
      while( false !== ($file = readdir($handle)) )
      {
         if( $file != "." && $file != ".." )
         {
            if(is_dir($strDirectory . "/" . $file))
            {
               doRecursiveFindAndInclude($strDirectory . $file . "/", $strFilename, $blnIncludeAll);
			}
			else
			{
			   $strCurrent = $strDirectory . $file;
			   $strTestFilename = $strDirectory . $strFilename . ".php";

			   $pathinfo = pathinfo($strCurrent);
			   $extension = $pathinfo['extension'];

			   if( $strCurrent == $strTestFilename && !$blnIncludeAll )
			   {
			   	  include_once $strCurrent;
			      echo "FOUND: " . $strCurrent;
			   }
			   else if( $blnIncludeAll && $extension == "php" ) //Don't include directories
			   {
			      include_once $strCurrent;
			      echo "INCLUDE: " . $strCurrent;
			   }
			}
		 }
	  }
      closedir($handle);
   }
}

?>
Advertisements

Posted in DIY | Tagged: , , , , | Leave a Comment »

Install bathroom sink drain

Posted by derekbobo on February 11, 2009

I’ve moved my blog. You can find this post at it’s new home.

Posted in DIY | Tagged: , , , , | 1 Comment »

Random Firings

Posted by derekbobo on February 9, 2009

It’s been a while since I had a chance to write. Let me get you caught up:

Snowstorm
We had some nasty weather the week before last. A combination of snow and ice knocked out the power for most of Southeast Ohio. I actually had about 3 days off of work due to the power outages and only being able to operate at a limited capacity.

Probably the more painful experience was that I was without power at home for 7 days. We toughed it out for the week by lighting candles and running a kerosene heater. It was extra tough because we had 3 sick dogs that we constantly had to take care of and try to keep warm. I pulled the grill up to the back door and we were able to boil water for dishes and do some cooking, but we ended up running in town for a meal about everyday.

The time without the normal ameneties did give me some good time to reflect. It puts into perspective the things that are important and how much time you waste. It thwarted efforts on personal projects, but also gave me a chance to evaluate what is important.

Car Problems
My car has been struggling for quite some time. Most of the issue I’ve had have been minor, but annoying. It started out with the dash lights not working and windows not rolling down. I recently smacked a deer with it causing some substantial body damage. A week or so later I started noticing the power steering acting up a bit and the transmission starting to slip (it had done this for a while… but is getting noticeably worse in a hurry). I was still driving it along but it was topped off when I got about half way to work and my seat decided to just randomly break.

My good luck continued when we decided to see a movie a couple weekends ago. Given the recent problems with my car and some poor weather we decided to take my old 4×4 truck. We got a couple miles down the road and it started to wobble a bit. I got it slowed down and checked out the left wheel and everything looked okay… but I decided I’d try to limp back to the house. We got about half way there before the wheel nearly came off the truck. I had hoped it would just be a ball joint or wheel bearing but as luck would have it tore up the whole front end.

I ultimately went and bought a 2002 Dodge Ram which is to serve as my new primary vehicle. It’s a very sharp truck, but doesn’t do so hot on gas. Now I gotta figure out what to do with the old junkers I have.

Solar Power
I have this dream of building a cabin that is off the grid and completely self sustaining. While I like the idea of helping the environment it’s really more about eliminating dependencies on utility companies and unnecessary luxuries (more on all of this at a later date).

My desire to start implementing this became more fueled with the week long power outage. I appreciate all the hard work that went into restoring power, but customer service at utility companies just disgusts me. They don’t treat you like a customer, they treat you like a number. They portray it as though you should feel privileged to be able to work with them and homie dont play like that.

I called in to report my outage and check the status. I told them I wasn’t expecting the power to come on immediately, I simply wanted to know if they were working on my area. Was I even in the queue. All they could do was quote me some generic recording that gave no indiciation on where their efforts were being focused. For all I knew they could have been 3 counties away. This type of response is not acceptable in other industries so what makes them unique? It makes me sick.

I’ve since been researching on how to get off the grid. I think it would be tough to do this at our current residence but it’s definitely possible for the cabin I plan to build. That aside I think it would be nice to get some sort of backup system implemented so I don’t get left in the dark during the next power outage.

My research has lead me to the following:

  • Solar panels mounted on the roof that provide charge to a battery bank
  • Solar panels feed to a charge controller which helps protect the batteries from over charging
  • Charge controller connects to the battery bank
  • Multimeter attached to the battery bank used for checking system status
  • Battery bank should be comprised of deep cell golf cart batteries
  • Battery bank connects to a DC to AC inverter which turns the energy into household electricity

At this point you can either wire this into a breaker box or hook up devices directly to the inverter. I’ve already started putting together some preliminary designs. I’ll probably order some stuff over the next few weeks and start doing some testing to prove out the concepts. When I get to this point I’ll keep some ongoing posts.

Work / Life
For the time being I’m going to try and restrict internet usage from home. I plan to continue to work towards my goal of starting my own business, but the house remodel needs some love right now. I’ve been really trying to get a better balance happening and it’s apparent that doing IT stuff all day (work) and all night (my business) is just too much. I’m going to try and pull the reigns back for now and focus on what’s important. Right now that’s getting our house to a respectable place. This should help make the wife happy and once we get caught up a bit I’ll be able to work on my projects again without feeling guilty.

Parting Shots
There was a ton more than the above that happened in the last couple of weeks, but I think this gives a good sampling of the chaos rattling around in my head right now. I’m going to try and get back into regular posts and will comment on the specifics as they come up.

Posted in Life | Tagged: , , , , , | Leave a Comment »