I started using CodeIgniter in January 2008, and it was a huge milestone in my development education. It showed me new ways to build, and I’d never be making the things I am today if it wasn’t for CI. I still love it and I still use it – it is getting better every day via Reactor, and it has a bright future.
That being said, PHP has changed a lot since CodeIgniter’s core was developed. A few months ago, some stuff happened and Dan Horrigan decided to create FuelPHP – a framework loosely in the tradition of CodeIgniter, but built to take advantage of the bells and whistles of PHP 5.3+.
I decided to really delve into FuelPHP over the weekend and found a lot of things that I really love and a lot of things that were really different in both syntax and method to what was done with CodeIgniter. So, coming from a tried and true CI guy, here are some observations of what to look out for when using FuelPHP.
Note: This isn’t meant to win people over to FuelPHP or espouse the virtues of switching, but it is meant as an overview of some major changes in thinking/structure for CI developers looking to use FuelPHP.
A Class is a Class
In CodeIgniter you have models, controllers, and libraries. You may notice that these are all regular old PHP classes, they are just treated differently by CI.
In FuelPHP, everything that is a class goes in the ‘classes’ folder. Controllers go in a ‘controller’ sub folder and models go in a ‘model’ sub folder. There are no libraries – they are just all classes and they all hang out in the classes folder. It’s a small change, but it may throw you off if you are used to having your controllers, models, and libraries lead separate lives.
PS: There are no helpers. They were dumb anyways. Which reminds me…
There are no Helpers
Most functionality that you’d find in helpers in CI is in classes in Fuel. You can still find old, completely useless friends like br() in the HTML class. What that’s you say? You use helpers for literally everything, even the simplest of tasks? Well, Fuel has an h tag function. Shit just got real.
Get Ready for Static Methods and Properties
You can use CI for years and never come across the concept of static methods and properties aside from defining parent classes for models/controllers. That’s mainly because in CodeIgniter, you were essentially dealing with a super object instance so everything uses the $this context in one way or another. That’s why if you wanted to “tap into” CI’s functionality in a library you needed to get a copy of it using $CI =& get_instance().
FuelPHP, you can use static classes. Here is a rough example. Instead of:
(Yes, CI does load the Uri class by default, but you get the idea.)
A whole thing on PHP static methods is beyond the scope of this article, but basically using classes as static allows you to access properties and methods without having to create an object context.
When you are loading a library in CodeIgniter, you are creating an object instance, and you are dealing with that object when calling properties and methods. In many cases with FuelPHP, you are accessing static methods using the scope resolution operator (::). One thing many CI coders may rejoice in: this means you don’t have to load a lot of classes.
You can, of course, instantiate classes as objects and several FuelPHP native classes require this, such as the validation class. The syntax for that will look a little more familiar:
$val = Validation::factory();
$val->add('username', 'Your username')->add_rule('required');
You’ll also notice if you are creating a class and using it as static, things that need an object context to work (like $this->) won’t work. You’ll need to use self:: instead.
If you have never used double colons before with it being a typo, then this will be a change. However, it’s something to force you do do some PHP self::educating. See what I did there?
Get Your Namespace On
Namespaces don’t come up at all in CI, mainly because they are really new (5.3+), but they play a larger role in FuelPHP. A warning: my knowledge of namespaces is shaky. So here we go.
In CI, you may have gotten an error telling you you cannot redeclare a class if you have a library the same name as a controller, for instance. Then you have to name your library something stupid and it ruins your day. Am I right?
Namespaces solve that problem by allowing you to encapsulate classes under a certain namespace. Want two classes named Chicken? You got it, if they are under different namespaces.
FuelPHP’s core files are all under the namespace Fuel. What do you care? Well, you’ll care because you’ll start using core Fuel classes in your own classes with namespaces and they won’t work. You need to tell PHP that you want the root namespace, but putting a
forward slash backslash in front of the method call:
There may be more to it, but to start that’s what you need to know.
So Much More
There are a lot more cool things to discover in FuelPHP (HMVC built in, REST built in, command line integration, etc), but those are the biggies if you are a diehard CI guy. Just remember – FuelPHP is still developing, so if you don’t see something laid out in the docs, dive into the code and the forums. For example, there is a form class, but it hasn’t been documented yet, so don’t be a baby and just look at the damn class already.