Add a semicolon at line end with Sublime Text

Two step process but oh so worth it ūüôā

First create a file in your User directory (on Ubuntu it’s /home/username/.config/sublime-text-3/Packages/User/, I believe) and name it¬†Line terminator and newline.sublime-macro. Open the file and put in the following:

{ "command": "move_to", "args": {"to": "eol"} },
    { "command": "insert_snippet", "args": {"contents": "${TM_LINE_TERMINATOR:;}"} }

Then open your key map file (Under Preferences > Key Bindings – User and put in

{
    "keys": ["alt+super+enter"],
    "command": "run_macro_file",
    "args": {"file": "Packages/User/Line terminator and newline.sublime-macro"}
}

(You can of course update the keys used to whatever works for you).

Then use it as you would with any key binding – it will automatically insert the semicolon at the end of the current line for you! ūüôā

Source: http://sublimetext.userecho.com/topic/90524-add-semicolon-and-newline/

Upgrading PHPUnit on (X)Ubuntu

When I recently installed Xubuntu 13.04 and added PHPUnit, apt-get installed version 3.6. This is not unusual; the repository is usually running an older version. I happened across this article on upgrading to a newer version and am posting it here (link to original article is at the bottom). All commands should be run via command line, of course:

sudo apt-get install phpunit
sudo pear channel-discover pear.phpunit.de
sudo pear channel-discover components.ez.no
sudo pear channel-discover pear.symfony-project.com
sudo pear upgrade
sudo pear upgrade phpunit/PHPUnit

This upgraded my installation from 3.6.x to 3.7.24 (as of this writing)

Source: http://konradpodgorski.com/blog/213/how-to-install-phpunit-on-ubuntu-11-10-and-get-it-to-work/

Your Perfect PHP Framework

With “Framework X rocks” and “Framework Y is the future of PHP” still being tossed around it’s ok to remember that the perfect framework for you is…

the framework *you* are most comfortable with and can code most quickly in. Whether it’s Yii, CakePHP, Litihum, Symfony, Laravel, Kohana, or CodeIgniter, find the one that “just fits” (and believe me, you’ll know which one works best for you) and get coding!

Note: There *is* something to be said for choosing the right tool for the job, and that is also a consideration (you probably wouldn’t use Slim to code an enterprise level intranet system, for example). Just don’t choose a framework only because it’s the newest thing, or the best known thing, or the flashiest thing.

Composer, Laravel, PHP-FIG, oh my!

It’s safe to say that my “Comparing Laravel, CodeIgniter, and Cakephp” post is the most popular entry this blog gets! I’m very glad that so many people have found it informative and relatively unbiased as they look for a “toolbox” to help them build their apps.

The downside is that it was written 9 months ago – which, in the world of programming/technology, can make a lot of difference! Since I wrote the original post we’ve seen Composer take off, PHP-FIG becoming more widely adopted, and Laravel being the “it” framework in anticipation of version 4. Because I have no desire to pit framework against framework again, let’s look at these three things ūüôā

Composer

Composer allows you to add packages to your application by adding a line to a (composer.)json file and running a simple command via command line. I was very leery of it at first but I’ve grown quite fond of it. Being able to update a Markdown or Swiftmailer package by opening my terminal and typing “composer update” is fantastic. Definitely beats the pants off trying to keep up on version number, downloading, extracting, hoping I didn’t miss something critical. The fact that it also snags the dependencies your package will need is pretty nice, too.

The only caveat I’ve found is that you do need to be aware of the dependencies, as it can quickly fill up your vendor folder with a lot of files that only that one package might use. Disk space is certainly cheap, and the number of files you use shouldn’t really matter, but it’s still a form of bloat, and could easily get out of hand — like, “Why do I have 310 folders…oh, that’s for the XYZ package”.

PHP-FIG

PHP-FIG is a group of leading PHP devs who are striving to help our code become more consistent, more interchangeable  and thus easier to read and easier for programmers to use. They have (at the time of this writing) 4 standards for coding РAutoloading, Basic Coding, Coding Style, and Logger Interface.

Many devs are up in arms that someone is “trying to tell me how many spaces to use”, that “Why can’t I use tabs?”, and “No one tells me how to code!”

Personally, I think the things laid out in each standard make sense insofar as creating smooth, consistent code readability. I don’t necessarily think that using snake_case instead of camelCase for function names really matters as long as you can figure out what the code is doing, but as far as the tabs thing goes – seriously, I forgot I was using spaces instead of tabs by the end of the first day. It’s extremely annoying to open a file from someone else and not be able to read it because their tabs are set at 5.5 and the code is all over the place.

So, I feel that we should put on our big girl/boy pants and just try to work together so we can ALL succeed.

Laravel 4

To say that Laravel 4 is on fire in the PHP world is an understatement. It hasn’t even reached stable release yet and has already been nominated for “Open Source Project of the Year” at the .net Awards. That’s an accomplishment!¬†I’ve gone through and played with the L4 code and I both am and am not impressed.

The fact that it has been broken into multiple components and those bits can, theoretically, be used independently of the framework¬†itself¬† is fantastic. The Support and Database packages are perfect examples of this, and I use both in various projects. Being able to swap other packages — for example, Cartalyst’s Sentry package — is also great. It’s interesting to see, too, how they have managed to create a way for non-static classes to be used statically (via the Facade pattern), maintaining testability while allowing easy access to often used classes (like Config, Lang, etc). That said –

L4 is not quite as decoupled as it might seem; for example, to use the Pagination package you need the Http, Support, and View packages as well as the Symfony Http-Foundation and Translation packages. That’s an awful lot of code for something that should be pretty simple, and doesn’t really lend itself to easily using it outside L4.

I also found it difficult to wade through the code to find out how something worked. For example, I wanted to have a dynamic action key in a route – something like

Route::get('foo/{wut}', 'Controller@{dynamic}')->where('wut', '(add|edit)')->where('dynamic', '(add|edit)');

It took me almost an hour to work out how L4 was setting up routes to realize that this was not going to work. I’m okay with it not working, I’m not okay with it taking me that long to follow the trail.

Other,¬†nit-picky¬†things – I don’t care for the huge App container that is passed around; it doesn’t feel much better than using global $app; or Class::$global or $this->ci->someclass, and makes it difficult when trying to debug one class (if that class has the App instance passed to it).¬†For the majority of the projects I’ve worked on lately, L4 feels huge and like too much.

All this is not to say that L4 is bad or not “newbie” friendly. If you just need a solid base to get you started, L4 is fantastic and can get you going quite fast. Package development is not very¬†difficult¬†either and lets you add to what’s already there.

As always, use what works best for you and your specific project. I would certainly not use L4 for a simple website for the local ice cream parlor, but I might consider it for a local school’s community site for their sports program.

Please note¬†I speak as someone who has programmed at least part-time since 2003 and used a framework since 2006, which is to say I’m by no means an expert but I have worked with a fair amount of code. These are simply my opinions on what I’ve experienced with my usage, and your own mileage may vary.

“Illuminating Your Laravel 3 Application”

“Illuminating Your Laravel 3 Application”

Cool article giving a brief overview of some of the changes/upgrades to Laravel 4

http://www.slideshare.net/aaronsaray/enterprise-php-mappers-models-and-services

http://www.slideshare.net/aaronsaray/enterprise-php-mappers-models-and-services

Great read-through for getting a handle on separating out concerns.

[Quick Tip] Add Composer Dependency Manager Support To Kohana

Composer is a super cool way to manage dependencies for your applications. Kohana’s autoloader does not support Composer’s way of autoloading, so if you’re wanting to use composer packages in your application you’re going to run into issues.

However, it’s really really simple to add support. Simply open your application/bootstrap.php file, and (I added this after Kohana registered its autoloader) add

require [FULL_PATH_TO_COMPOSER_FOLDER].’autoload.php’;

Where, of course,¬†[FULL_PATH_TO_COMPOSER_FOLDER] is the actual absolute path to Composer’s auto-generated auto-loader files.

Voila! You should now have Composer support in your Kohana app! ūüôā

(CakePHP) Get a Database Connection Instance

In my current project I am working on, I am using CakePHP and need to fetch some data from the database very early in the request cycle (don’t worry; it’s cached to avoid unnecessary queries).

In past versions of Cake you could use ConnectionManager::getInstance(). However, in 2.2 (and possibly in earlier versions of 2.x; I do not have the time to check), this no longer works.

The solution is to use the getDataSource method. So, by calling ConnectionManager::getDataSource('default') I can retrieve an instance of the default database connection and go along my day. Yay!

Quick Little Tip Regarding Cakephp forms

Occasionally when I am coding something in Cake that requires a form, I run into a problem if I use something like

if( !empty($this->data) ) ...

If there is another method that posts to the URL that the above code rests on (an Auth class trying to preserve the page the person was trying to reach, for example), then chances are good the method will think the user is trying to post to that method as well.

Anyway, my quick solution to ensure that only the data *I* want is posted is this:

if( !empty($this->data['ModelName']) ) { ..

This ensures that the method is only looking for the data that we want to post to it.

Twilight

My sister is in her early 20’s and is a die-hard Twilight fan. She owns all 4 books and has been bugging me for months to read them. So last week she brought them over and yesterday I opened the first book and started reading.

Now, I should mention that when it comes to ultra-popular fads I tend to stay far back until some of the hubub dies down. I didn’t start reading Harry Potter until my sister took me to see the first movie. And I’d heard pretty horrible things about the Twilight series anyway, in terms of literary-ness.

I was surprised to find that I did enjoy the story, enough to finish reading the first book in one day. I liked the ideas (most of them – I mean, sparkly vampires..C’MON!! lol) and the basic premise of it all.

That being said..

The writing is horrid. I’m sorry if I offend any fans out there, but I can’t believe that the book was published the way it was. I couldn’t turn around without tripping over a cliche. The main character, Bella, was flat and boring. She seemed to have no desires to do anything outside of be with Edward. She seemed to have no interests, nothing. She seemed whiny and a complete doormat to anyone around her.

And what did the author do, pull out a thesaurus to use as many $0.50 words as possible? I started outlining a drinking game to take a drink every time Bella described one of Edward’s features as “marble”, only to realize it would likely end up giving someone alcohol poisoning.

Don’t get me wrong – I really did like the premise of the book and the story itself. I just felt that it was really badly executed. If they went back and revised it a couple of times I think it would be a very compelling and powerful book.

Just my opinion, though.