Phalcon 2.0 Alpha Landing

Tweet

I’ve written about Phalcon before, and we’ve got a variety of articles on the framework published already, which is apparent if you just visit the Phalcon category. In fact, not so long ago, I wrote about Zephir, Phalcon’s noble initiative to make building PHP extensions accessible to everyone.

Today, a new milestone has been reached by the team, and one that definitely warrants discussion. Way ahead of time, the Phalcon team announced the imminent release of Phalcon 2.0 alpha 1.

Phalcon 2

Phalcon 2 is a full rewrite of the original Phalcon Framework, in their new language, Zephir. As previously discussed, Zephir is a mixed-type derivative of PHP (both static and dynamic types are supported) which allows developers to write “almost-PHP” code and compile it down to C based extensions for PHP, installable like any other (iconv, imap, imagick, etc). Phalcon 2 was built almost entirely in this language, and is now easier than ever to maintain, update and above all – accept contributions from the community. As proof of working concept, the Phalcon team is running their site on Phalcon 2 here.

Phalcon 2.0 is being brought up to speed with the current version 1.3 (it introduced some optimizations not present and yet required in 2.0), and is being built around existing 1.3 classes to ensure backwards compatibility. To see which classes were ported so far, observe the following table. If you’d like to assist in the transfers, submit a pull request as you see fit – particularly unit tests. Most of the ported components simply haven’t been tested yet, even after being fully developed.

Zephir and creating extensions

Zephir is now in version 0.3, and can be used by the general public to write custom extensions for PHP. Zephir supports all the standard logical structures you’re used to (except “else if”), and lets you pre-declare variables by type to make it easier to compile into highly optimized C code. It’s important to note that Zephir is an ahead-of-time compilation language, meaning the compilation step might just make you less productive if you’re used to iterating and testing rapidly. The long term gains, however, are worth it.

Zephir is memory-safe, meaning it has no pointers or manual memory management support. For such low level operations, one should stick with C. With regards to power, Zephir is thus weaker, but more user friendly than C. You can consider it a hybird of C and PHP, leaning more to the PHP side of things.

So what can I do with it? Always wanted to close-source your PHP app? Wondered how much faster a statistics calculation of your company’s backend would be if it were running in C? You can do all that and more now. First, check out this Vimeo screencast on making your first Zephir extension.

Creating your first PHP extension with Zephir from Phalcon Framework on Vimeo.

Next, follow through the tutorial, read the docs, and play around. If you end up making anything other than what the tutorials teach, we’d love to publish your step-by-step.

An example Hello World class like this one:

namespace Test;

/**
 * This is a sample class
 */
class Hello
{
    /**
     * This is a sample method
     */
    public function say()
    {
        echo "Hello World!";
    }
}

will compile down into this one:

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "php.h"
#include "php_test.h"
#include "test.h"

#include "kernel/main.h"

/**
 * This is a sample class
 */
ZEPHIR_INIT_CLASS(Test_Hello) {
    ZEPHIR_REGISTER_CLASS(Test, Hello, hello, test_hello_method_entry, 0);
    return SUCCESS;
}

/**
 * This is a sample method
 */
PHP_METHOD(Test_Hello, say) {
    php_printf("%s", "Hello World!");
}

Note that Zephir requires you to use classes and namespaces – code without them is not allowed – this is to enforce good coding habits, proper object oriented code and solid encapsulation.

Contributing

You can contribute to the faster development and better test coverage of Phalcon 2.0 by installing as usual, only doing so from the 2.0.0. branch. Note that this will replace your current Phalcon installation – so only use it in development environments, preferably something like Vagrant boxes. For more details on pulling off a Phalcon install in a Vagrant VM, see this article.

The full installation procedure is as follows:

git clone http://github.com/phalcon/cphalcon
git checkout 2.0.0
cd ext
sudo ./install-test

In any *nix environment, this just clones the usual repo, switches to the 2.0.0 branch, and then installs as usual. The API is, for the most part, identical to what it was, so transitioning shouldn’t be very difficult – and if you encounter any bugs, the Phalcon team encourages bug reports on the GitHub issues page, preferably followed by Unit Tests that demonstrate the bugs. For a good overview of contributing to an open source project on GitHub, please see here and here.

To further contribute to Phalcon effortlessly, consider upvoting this issue as well – it aims to have Phalcon installed on Google App Engine, as described here, and would produce the most powerful PHP environment to date, outperforming even HHVM.

Conclusion

Phalcon is moving ahead faster and better than anyone expected. We can expect to see the full stable release by Q2 2014 for sure, marking a year of Zephir’s development and Phalcon2’s announcement. The framework will probably be safe to use in production apps long before that.

If you’re experimenting with Phalcon 2 or Zephir, please give us a shout – we’d love to publish some tutorials you write. If you just have an idea, or have built something but feel like you don’t write well or your English is bad, get in touch nonetheless – we’ll find a way.

Free book: Jump Start HTML5 Basics

Grab a free copy of one our latest ebooks! Packed with hints and tips on HTML5's most powerful new features.

  • http://mikewink.de/ Mike Wink

    Thank you very much for the article Bruno. I have Phalcon on my radar for a long time, but never made any substantial projects with it. With the upcoming 2.0 release I will definitely join the community porting some former Drupal “applications” to it.

    There are two small problems. The Google Apps Engine and HHVM link don’t work.

    • http://www.bitfalls.com/ Bruno Skvorc

      Thanks for the heads up! FIxed!

  • http://jream.com Jesse

    I have been using Phalcon for 4 to 5 months now, It’s my favorite framework I’ve ever laid my hands on. Very good article, I’m glad it’s getting more attention :)

    • http://www.bitfalls.com/ Bruno Skvorc

      Cheers! And thanks for the feedback!

  • http://www.matthewsetter.com/ Matthew Setter

    “Note that Zephir requires you to use classes and namespaces – code without them is not allowed – this is to enforce good coding habits, proper object oriented code and solid encapsulation.”

    this really says it all to me. Expect a higher standard, make it not quite so easy to start, and get a better quality of developer and as a result, project. It’s looking like an excellent project to be involved with.

  • http://www.bitfalls.com/ Bruno Skvorc

    You should be able to, yes – the APIs are mostly the same. I haven’t tried, but give it a go and let us know :)

  • Szabolcs Zajdó

    Wow, very interesting framework, will definitely follow it’s development in the future! :)

  • http://superhero.com Frederik87k

    Hei! Just got started working with this incredible powerful framework. Shall I go with the most latest release 2.0.0 which right now includes 86% of version 1.3, or maybe with version 1.3 as it includes recent fixes, OR sould I go for version 1.2.5 as this one is recommended as “stable”?

    • http://www.bitfalls.com/ Bruno Skvorc

      If you’re not afraid of living on the bleeding edge and being as up to date as possible, go with 2.0. Otherwise, go with 1.3.

      • http://superhero.com Frederik87k

        Thank you for your advice! I’ll go with version 1.3 for now.

        I guess I upgrade to v2.0 as it leaves its alpha-state. :)