Phalcon 2.0 Alpha Landing

Share this article

I’ve written about Phalcon before, and we’ve got a variety of articles on the framework published already. 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.

Bruno SkvorcBruno Skvorc
View Author

Bruno is a blockchain developer and technical educator at the Web3 Foundation, the foundation that's building the next generation of the free people's internet. He runs two newsletters you should subscribe to if you're interested in Web3.0: Dot Leap covers ecosystem and tech development of Web3, and NFT Review covers the evolution of the non-fungible token (digital collectibles) ecosystem inside this emerging new web. His current passion project is RMRK.app, the most advanced NFT system in the world, which allows NFTs to own other NFTs, NFTs to react to emotion, NFTs to be governed democratically, and NFTs to be multiple things at once.

frameworkphalconPHPphp frameworkzephir
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week