| 26/01/2013, 19:31 PHP Code Style
I recently decided I wanted to apply the PSR-2 strict coding style to my existing PHP code and classes and as result surprised myself how quickly I was able to adopt it naturally in the new code I was writing once I had some similar configuration set across text editors.
I have been a user of emacs for a number of years (as well as jEdit, TextMate and more recently Sublime Text 2) and with it's default indent style and my personal tendency of having curly braces on separate lines my PHP would typically have a form similar to this arbitrary example:
class front extends UserController
{
public function default_event()
{
if($this->isXMLHttpRequest())
{
header('Content-type: application/json');
$message = '';
if(isset($_POST['code']) && ctype_digit($_POST['code']))
{
$messages_arr = array(
'May onions grow out of your ears for two years!',
'May the last page of the next book you read turn blank so you never find out the ending.',
'May your next white wash have a red sock in it.',
'May the dog eat your homework.');
$code = (string)$_POST['code'];
if(strlen($code) == 5)
{
$message = $messages_arr[rand(0, sizeof($messages_arr)-1)];
}
echo json_encode($message);
}
else
{
echo json_encode('');
}
exit();
}
if(isset($_POST['forward']) && $_POST['forward'] == "1")
{
$this->redirect('login');
}
}
}
In order to do away with tabs altogether and use a standard four space indent, I needed to change some basic settings for emacs. I then wanted a method for applying this indentation across a whole file at will. I came across this answer to a Stack Overflow post to help me with that.