Joseph Hallenbeck


December 29, 2013

Photo Journal #7 – Bear Lake Panorama

Filed under: Photography — Tags: — Joseph @ 11:31 pm

Flattr this!

Bear Lake Panorama

I have taken to experimenting with stitching panoramic images together. The one above is of Bear Lake in September of this year. We sped around the lake at sunset hoping to get to the eastern shore in time for the shot. I ran out of the truck down onto the stony beach and started shooting.

The resulting photo has has had little post work done in lightroom, but was stitched together using photoshop with a dash of content aware fill to fill in where the distortion curved down into the blue sky and took out a chunk of the right-most mountain.

Settings

Model: Nikon D80 /w Nikon 35mm f/1.8 AF-S DX Shutter: 1/160 sec
Exposure Program: Manual
F-Stop: f/9
ISO: 400
Focal Length: 35mm
Lighting: None
No of Stitched Photos 4


December 2, 2013

My Favorite PHP Helper Function — A Better Isset()

Filed under: Software Development — Tags: , , — Joseph @ 10:44 am

Flattr this!

My favorite helper function for CodeIgniter is a ridiculously simple function that has an amazing amount of utility. I stole, at least the idea of, this function from LemonStand and it has since made its way into nearly every CMS that I have worked on:

/**
*  Check if $var is set and if not return null or default
*  @param mixed $var The var to check if it is set.
*  @param mixed $default The value to return if var is not set.
*/
function h( &$var, $default = null) {

    return isset( $var ) ? $var: $default;

}

At first this doesn’t really seem to be doing much, after all at first glance it looks like it is nothing more than a wrapper for isset, but this improves heavily upon isset in two very important ways. First, let’s look at how this function works.

In the function definition we are taking a reference to a variable. Recall, a reference is pointing at the memory, not value, of a variable and so we can actually pass to our helper function a variable that has not yet been initialized. This saves us from receiving a notice that the variable does not exist. Our call to isset thus checks our memory to see if it is actually referencing a variable or nothing at all. If it is referencing an actual variable it returns that variable, otherwise it returns null (our default default) or whatever value has been assigned to $default.

The utility of this is best expressed in handful of examples. The biggest use of this is in a view. Let us look at a view constructed without the use of our helper function:

<h2><?= isset( $title ) ? $title : 'Blog Title' ?></h2>
<p><?= isset( $content ) ? $content : null ?></p>
etc.

In a sizable view the above can get quite long and quite cumbersome to maintain. Each call to isset is checking to see if the controller actually passed the value on to the view ( $title or $content ). If we did not do this we would get a notice from PHP. Sometimes this is resolved by programmers by using the error suppression symbal (@), however the notices will still end up in the logs of many frameworks that strictly check for errors. Contrast this with a view using our helper function:

<h1><?= h( $title, 'Blog Title' ) ?></h2>
<p><?= h( $content ) ?></p>
etc.

The above is a much, much more concise view that is easier to read and is still a strictly valid snippet of PHP that generates no warnings or notices. Once we start to use this helper function regularly all different kinds of uses come up for it, for example we can use it to see if a model returned a value:

# 1. Longer method without using the helper function
$page = $this->pages->getByURI( $url );
if( ! $page )
{
    $page = $this->pages->get404();
}  
$this->render( $page );

# 2. With helper function
$page = h( $this->pages->getByURI( $url ), $this->pages->get404() );
$this->render( $page );

The above snippets are fairly simple, but let’s walk through them. In both instances we need to pass some page object on to the render method. An error occurs if it does not get a valid page object so we must check after retrieving a page that it actually exists. In the first snippet we use four lines of code to first get a page by $url (the value of which is set somewhere else). Now if the pages model returns nothing then we enter a conditional statement that retrieves the 404 error page.

However, with the use of our helper function we can shorten the code in half and remove the conditional all together make it a much more readable snippet of code. The first line of the second snippet simply passes the return of the pages model and the get404 method into our helper function which returns the first if it returns something or the latter if it does not. The only downside is the additional load since the 404 page would also need to be loaded concurrent to the current page with each request, but in most cases this is going to be negligible.

Having looked at two different uses for our helper function, we can begin to see that we can get quite a bit out of some very very small functions. If you have your own favorite one-liner functions feel free to share in the comments below.




Copyright 2011 - 2017 Joseph Hallenbeck Powered by WordPress