PHP Quick Tips – Fruit of Framework Hopping

Zend Framework and Kohana v3 – they were my favorite PHP frameworks and I’ve learn a lot from them, especially from Kohana v3. I have gathered some nice tricks that I never knew until I discovered them on these frameworks. To not being biased, I will not name from what framework it came from, not unless it is framework specific.

Note: I’m just writing my observations and my views may change in the future, so don’t take this post seriously.

Date Format in Zend Framework

The date format in Zend Framework is different from PHP. What I mean is that the format “Y-m-d” from PHP is different from Zend Framework. When you look at the Zend Framework documentation for dates, they are using some sort of ISO standard (I don’t dig the details).

Okay, let’s do it quick. If I want to validate against a format like this: “2010/01/05” – (that is Jan 5, 2010) in PHP, the format is Y/m/d, however in Zend Framework, I can immediately think it is “YYYY/MM/DD” which is wrong.

So from now on, I shall never forget this. The correct format is: “YYYY/MM/dd” – notice the small letter d’s. Applicable on output and validations.

Appending an array to another array

This was cool. So you have an array that may look like this:

$data = array(
  'id' => '1',
  'name' => 'My name',
  'gender' => 'Male'
);

And you have another array that may contain the address, telephone number, etc or even replace the old name with the new name on the second array for example:

$data = array(
  'name' => 'New name',
  'address' => 'Male',
  'telephone' => 'None'
);

To append or merge them, there is so called array_merge from PHP. However, it is a function and is messy (not cool :D). So let’s do it in cool way. Use the addition operation.

$data = array(
  'id' => '1',
  'name' => 'My name',
  'gender' => 'Male'
);

$data += array(
  'name' => 'New name',
  'address' => 'Male',
  'telephone' => 'None'
);

That way, it appends the new array to the old array and replaces the value of the old field if that field also exists on the second array.

Cast a variable into array

Are you tried of the foreach warning/errors when the passed argument is not really an array? Are you tired of checking the variable first if it is an array before using foreach on them? Is it as messy as this?

if (!empty($items) && is_array($items))
{
  foreach ($items as $item)
  {
    // do your thing
  }
}

We can remove the extra indention by casting the variable into an array. This is also useful when you are accepting an array parameter but also allows passing non array which will be converted into an array with single element.

$items = (array)$items;
foreach ($items as $item)
  // do your thing
}

Using echo to output HTML but maintain readability

Well, you can always do better. However, I found this cute. Using echo to output HTML elements, especially in loop, is messy and is not so readable. I have devised ways but after I looked upon somebody’s code on the internet, I think it’s not that bad. Using the commas for out echo, we can make it more readable using indentions.

$w = ' width="' . $first_thumb['width'] . '"';
$h = ' height="' . $first_thumb['height'] . '"';

// output video player link
// get button position
$button_position = Player::button_position($first_thumb['width'], $first_thumb['height']);
$button_style = ' style="top: ' . $button_position['top'] . 'px; left: ' . $button_position['left'] . 'px;"';
echo '<div class="player-thumb">',
		'<a href="' . HTML::chars($first_thumb&#91;'link'&#93;) . '" class="play-trigger" id="play_alt_', $feed_content&#91;'id'&#93;, '" title="Play video">',
			'<img src="' . $first_thumb&#91;'url'&#93; . '"' . $w . $h . ' alt="' . $title . '" />',
		'</a>',
		'<div class="player-button"', $button_style, '>',
			'<a href="#" title="Play video" class="play-trigger" id="play_main_', $feed_content&#91;'id'&#93;, '">',
				'<img src="', URL::site('/media/img/playbutton.png'), '" alt="Play video" />',
			'</a>',
		'</div>',
	'</div>';
// output player but invisible by default
echo '<p class="player" id="player_', $feed_content&#91;'id'&#93;, '">',
		$first_thumb['player'],
	'</p>';

Although you may argue that it looks ugly, but I found it cute, maybe it’s just me. Typing the opening and closing PHP tags is uglier.

I think that’s all for now. The rest of the things I’ve learned on those frameworks are usually more customized and is sometimes against the principles of those frameworks. It is like applying Zend Framework to Kohana v3 and vice versa. I have even a mini-Sprig port to Zend Framework which uses Zend_Db as the database helper. I’m planning to release it on github as long as I have time.

This entry was posted in Kohana v3, php, Zend Framework and tagged , . Bookmark the permalink.

One Response to PHP Quick Tips – Fruit of Framework Hopping

  1. Pingback: PHP Quick Tips – Fruit of Framework Hopping | Lysender | Programming Blog Imagik.org

Leave a Reply

Your email address will not be published. Required fields are marked *