Daniel Kendell

Programming

Main Config with Zend_Application

by Daniel on Nov.02, 2009, under Programming

Mainly a note to myself, but it’s handy to know.

When you instanciate Zend_Application, you provide it with a path to the config file (Or an instance if you’re feelin’ kinky) Zend_Application loads this into a Zend_Config object and does it’s bit with it.

What’s less obvious is how to retrieve that config from within a Zend_Controller_Action::method().

My solution until recently was to have an _initConfig() in my bootstrap class that read in the file again, and stored the instance in a Zend_Registry entry. However I think I cracked the “proper” way or “better” way to do it.

<?php

class MyController extends Zend_Controller_Action
{
	/**
	 * @var array The application configuration
	 * @access protected
	 */
	protected $_config;

	/**
	 * Init
	 *
	 * Retrieve and assign the application configuration
	 *
	 * @access public
	 * @return null
	 */
	public function init()
	{
		$this->_config = Zend_Controller_Front::getInstance()
			->getParam( 'bootstrap' )
			->getOptions();
	}
}

?>

Edit: In that particular example there is actually an instance of Zend_Controller_Front closer to hand, you could use $this->_frontController. But I’ll stick with ::getInstance() for the example since since it gets the job done.

Leave a Comment :, , more...

You’re Doing It Wrong: Branches

by Daniel on Nov.19, 2008, under Programming, Soapbox

A fairly straightforward thing you’d think. And you’d be right. But consider the following example.

if ( isset( $a ) )
{
    if ( isset( $b ) )
    {
        if ( isset( $c ) )
        {
            echo $a, ' - ', $b, ' - ', $c;
        }
        else
        {
            die( 'need $c' );
        }
    }
    else
    {
        die( 'need $b' );
    }
}
else
{
    die( 'need $a' );
}

As you can see, the actual operation here is the echo, and it’s nested three levels deep. If it were in a class method, then it’s still three levels deep, but it’s tabbed across the screen five times already.

Imagine this was a far more complicated piece of code and and it was a good four or five “screenfuls” it starts getting a bit challenging keeping track of all the different levels of branches and where you are in them. You end up doing a lot of scrolling up and down to remind yourself.

Why not adopt a “return-early” coding style instead? You check for what would stop your code working as opposed to what it needs to work. The above example has been rewritten to return-early style instead and now the echo statement isn’t in any branches at all.

if ( !isset( $a ) )
{
    die( 'need $a' );
}

if ( !isset( $b ) )
{
    die( 'need $b' );
}

if ( !isset( $c ) )
{
    die( 'need $c' );
}

echo $a, ' - ', $b, ' - ', $c;

What you have now is three small branches or chunks of code on screen that can be modified and rearranged a lot easier than they would be if you had multiple nested branches.

I guess the moral of the story is to keep as much of your code as far left of the screen as you can.

Leave a Comment :, more...

You’re Doing It Wrong: Tabs

by Daniel on Nov.15, 2008, under Programming, Soapbox

This is likely the first in a series of You’re Doing It Wrong posts that I have rattling around my head.

Today, using Tabs to indent code. As i’m sure we’re all aware, the wonderful thing about tabs is that you can set how wide you want them to be displayed. I like a tab width of four, others like two, some like eight. And all the time that we indent our code with tabs then everyone can be catered for when it comes to their whitespace preferences.

Like most things, there is a dark side. When you start to turn to the dark side, you start use tab characters to create structured whitespace in amongst your code, that being non-tab characters, followed by tabs, followed by further non-tabs.

It’s a bit difficult for me to demonstrate this adequately on the bloggenator, so take a look at this example.

And now your work of art is ruined. Editors are trying to be too clever for our own good. Yeah it’s great if i’m the only one who’s working on the code, but what if someone else joins me on it? Or someone else takes over the project? Yeah sure you could say “their problem, not mine”, but it’s not very considerate is it?

By sticking to a rule of only using tabs at the beginning of a line, your code will have the same presentational layout whatever editor I, or anyone else want to use to view it.

When someone else eventually takes over my code, and it’ll happen, the last thing I want them to do is throw it all out. If they see bits of lines of code all over the place because they use eight spaces and you use two, it looks a mess. That gives them a bad impression of you’re coding competence, unjustly so, since if they tweaked their editor it would look good again.

Personally I use a variable-width font at work and a fixed width font at home. Don’t ask me why I do, i don’t know. The editor background on my work editor is white, at home it’s black! How whacky is THAT?!

Leave a Comment :, more...


Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!