FuelPHP for CodeIgniter Developers

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:

$this->load->library('Uri');
$this->uri->segment(1);

you use:

Uri::segment(1);

(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:

\Auth::check();

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.

Happy coding!

This entry was posted in Code and tagged , , . Bookmark the permalink.
  • http://dhorrigan.com Dan Horrigan

    Great article Adam. You nailed most of it. Regarding the namespaces:

    Everything in the core is namespaces so that there are no naming conflicts and to make extending the core super simple. All you have to do is create the class in the app/classes folder with the same name as the class you want to extend. Then you just extend the core class:

    class Form extends FuelForm

    Also, a major difference CI people will have to get used too is that you don’t have to manually load anything. Everything is autoloaded, so you just use th class.

  • http://www.adamfairholm.com Adam Fairholm

    Thanks, Dan! Thanks for the point about the namespaces too. They can seem a little confusing at first to coders who aren’t familiar with PHP 5.3, but they are really handy in Fuel.

  • Gary

    Thanks for the article, Adam. I have a question: I’ve been developing an app using CI 2.0, and have been pretty happy with the functionality, but I am considering migrating to Fuel. Can you give a rough idea of how much effort it takes to change CI syntax to that of Fuel. Is it mostly the same or are will I be needing to update a lot of code? I’m not using any custom helpers, but I am definitely using a bunch of CI’s internal helpers, libraries, etc. Thanks!

  • Anonymous

    As somebody who works on both projects, if you are enjoying using CI 2.0 then you might as well stick with it. It also depends if it is just you, how long you have to work on it and various other factors.

    For example, I love that some of my projects run on CodeIgniter because third parties and my (now ex-)colleagues can work with it really easily, but if I used FuelPHP they would be confused as hell.

    Basically I don’t want everyone to see FuelPHP as a logical next step and leave CodeIgniter because they are both great frameworks.

    And yeah, you’ll be doing a lot of recoding :)

  • http://twitter.com/adamfairholm Adam Fairholm

    Gary – I agree with Phil on not abandoning CI is you like it (although Fuel is a lot of fun). All my CI stuff is staying CI for various reasons, but I have ported one module from CI to Fuel and this is what I found:

    A lot of the functionality can be changed with just a search and replace. For instance, $this->lang->line gets replaced with Lang::line. A lot of the often used functionality like that is conveniently similar.

    The thing that is the trickiest is the database functionality. Fuel’s DB class is ported from Kohana, and has some differences that go beyond simply changing the syntax. For instance, you can’t pass an associative array to insert – you have to pass an array of columns and an array of values. I ended up changing that core functionality to be more like CI to make the porting easier.

  • Gary

    Thanks Adam and Phil for the advice. Much appreciated. I will definitely be keeping an eye on Fuel for something in the future.

  • http://frenky.net Frank de Jonge

    Nice article, just one thing you might have missed.

    Since FuelPHP is for PHP 5.3+ context of static function and variables are different. Since self:: refers to the class (and every child or parent of that class) you might use static:: more often. With the static:: approuch a class can set it’s own static variables without interfering with the other childs of parents of the class.

    Nice work on the article!

  • http://zackhovatter.com/ Zack Hovatter

    Awesome article. I just started on FuelPHP today and this helped me get a better initial grasp on things.

  • Pingback: Top 7 PHP Frameworks | Devlounge

  • Pingback: 从CodeIgniter到Fuel | 资源分享

  • Xmory

    Thanks a lot, good article! I have translate this into Chinese in http://xmory.com/codeigniter-fuel/.
    Thanks again~

  • http://twitter.com/adamfairholm Adam Fairholm

    Awesome – thanks Xmory!

  • Travis Watson

    Adam, this is a very well written article on the topic, and I appreciate your insight and wit.  I have a project I’m pursuing with colleagues that are all very open minded, but have a strict OO background in C#, and I’m looking for a good OO MVC framework to ease the boundary.

    I originally suggested CI, but both Kohana and FuelPHP are looking great.  My only hesitation at this point would be the development cycles.  CI comes from a corporate environment, so I think Ellis understands the importance of backwards compatibility persistence through security and performance updates.

    From your article, and some things I’ve seen ’round the tubes, Fuel seems to have a better foundation, better developers, more cohesion, and a firmer direction than Kohana.

  • http://twitter.com/adamfairholm Adam Fairholm

    Thanks Travis! I appreciate it. I have no experience with Kohana, but I’d say that CI is not really coming from a corporate environment anymore – it’s currently being pushed and pulled like nobody’s business on GitHub. There are truly solid fantastic devs on both projects (some even work on both). So if you haven’t seen it I’d recommend checking out the CI repo: https://github.com/EllisLab/CodeIgniter

  • Pingback: Oubliez CakePHP, Codeigniter, Symfony… voici FuelPHP | Trois Point Zéro - webdesign, développement et actualité autour du web par Grégory BABONAUX, concepteur web freelance

  • http://www.facebook.com/daur1kz Dauren Dauletov

    Nice article, thanks.

  • Ricky

    Static classes are bad. You can’t pass them around as data. They make unit testing more difficult and less beautiful.

  • Ricky

    TL;DR: Use Symfony2

  • Robin Bullock

    Very helpful article, thank you!

  • Jerry

    Very informative article, and everything was good, until you called this a “forward” slash:

    Auth::check();

    Did you mean to use a backslash, but called it a “forward” slash, or did you mean to use an actual forward slash but used a backslash by mistake?

  • http://twitter.com/adamfairholm Adam Fairholm

    Funny – this article has been up for more than a year and no one (including me, obviously) noticed that! I indeed meant a backwards slash, and the article has been updated. Thanks Jerry!

  • http://abbotfrost.webnode.com/ Linus Snyder

    FuelPHP – a Structure usually in the customized of CodeIgniter, but developed to take advantages of the devices of PHP 5.3+.The above information by offering a brief information and enabling the client know about the fuel php for CodeIgniter Developer .
    .

  • Anonymous

    Thanks for that post! :) I was looking for an HMVC + ORM framework to switch to. FuelPHP looks exactly like what i need