PhalconPHP: Yet Another PHP Framework?

SitePoint Sponsors

There’s a wide offering of PHP frameworks, from full-stack frameworks containing ORMs, validation components, and loads of HTML helpers, to micro frameworks which go little beyond offering routing functionality. They all claim to be special, either with beautiful syntax, high speed, or good documentation. One of those frameworks is Phalcon. But Phalcon really is quite different compared to the other frameworks; it isn’t just another package that you download, rather it’s a PHP module written in C.

In this article we’ll take a brief look at what Phalcon looks like and what makes it so special.

What is Phalcon?

Phalcon is a full-stack framework. It promotes the MVC architecture and offers features like an ORM, a Request object library, a templating engine, caching, pagination… a full list of features can be found on it’s website.

But Phalcon is somewhat unique because you don’t just download an archive and extract it to a directory like you do with most other frameworks. Instead, you download and install Phalcon as a PHP module. The install process doesn’t take much more than a few minutes, and installation instructions can be found in the documentation. Also, Phalcon is open-source. You can always modify the code and recompile it if you want.

Compiled for Better Performance

One major drawback for PHP is that on every request, all files are read from the hard drive, translated into bytecode, and then executed. This causes some major performance loss when compared to other languages like Ruby (Rails) or Python (Django, Flask). With Phalcon the whole framework already is in RAM, so the whole set of framework files don’t need to process. There are benchmarks on the website that show indeed this has some significant performance advantages.


Phalcon serves more than double of CodeIgniter’s requests per second. And when you look at the time per request, Phalcon takes the least amount of time to handle requests. So whenever a framework says that it’s fast, think that Phalcon is even faster.

Using Phalcon

Phalcon offers the classic features of a modern PHP MVC-framework (routing, controllers, view template, ORM, Caching, etc.), so there is nothing special when compared to other frameworks except for its speed. Still, let’s take a look at what a typical project using Phalcon looks like.
First, there’s usually a bootstrap file which will be called on every request. The requests are sent to the bootstrap via directives stored in an .htaccess file.

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]

The Phlacon documentation suggests using the following directory structure:


But the directory layout can be modified if you really want since everything will be accessed via the bootstrap file which exists as public/index.php.

try {
// register an autoloader
$loader = new PhalconLoader();

// create a dependency injection container
$di = new PhalconDIFactoryDefault();

//set up the view component
$di->set('view', function(){
$view = new PhalconMvcView();
return $view;

// handle the request
$application = new PhalconMvcApplication();
echo $application->handle()->getContent();
catch (PhalconException $e) {
echo "PhalconException: ", $e->getMessage();


The controllers and models are autoloaded, so you can just create files and use them from anywhere in the project. Controllers should extend PhalconMvcController and models extend PhalconMvcModel.

Controller actions are defined like so:

public function indexAction() {
echo 'welcome to index';

Models too are pretty straight-forward:

class Users extends PhalconMvcModel

By extending the PhalconMvcModel class you immediately have access to some handy methods, like find(), save(), and validate(). And you can use relationships like:

class Users extends PhalconMvcModel
public function initialize() {
$this->hasMany('id', 'comments', 'comments_id');


Views offer basic functionality like being able to pass data to your views and working with layouts. Phalcon views doesn’t use special syntax though like Twig or Blade, though. They use pure PHP.

<title>Blog's title</title>
<?php echo $this->getContent(); ?>

Phalcon does however has a flash messaging system built-in:

$this->flashSession->success('Succesfully logged in!');

Phalcon Query Language

Phalcon has its own ORM, Phalcon Query Language (PHQL), which can be used to make database interaction more expressive and clean. PHQL can be integrated with models to easily define and use relationships between your tables.

You can use PHQL by extending the PhalconMvcModelQuery class and then create a new query like:

$query = new PhalconMvcModelQuery("SELECT * FROM Users", $di);
$users = $query->execute();

And instead of such raw SQL, you can use the query builder like this:

$users = $this->modelsManager->createBuilder()->from('Users')->orderBy('username')->getQuery()->execute();

This can come in quite handy when your queries get more complicated.


Phalcon offers the classic features of a modern PHP MVC-framework so it should be comfortable to use, so in that sense it is just another PHP framework. But where it really stands out from the others is its speed. If you’re are interested in learning more about Phalcon, check out the framework’s documentation. Make sure you try it out!

Image via Fotolia