PhalconPHP: Yet Another PHP Framework?

Share this article

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-01 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

Frequently Asked Questions (FAQs) about PhalconPHP Framework

What makes PhalconPHP different from other PHP frameworks?

PhalconPHP is a high-performance PHP framework that is implemented as a C extension. This means it’s compiled and runs at the system level, which makes it incredibly fast. Unlike other PHP frameworks, PhalconPHP doesn’t need to be interpreted at runtime, which significantly reduces overhead. It also offers a lower memory footprint, making it an excellent choice for high-traffic websites.

How do I install PhalconPHP on my server?

Installing PhalconPHP involves compiling it as a PHP extension. This process varies depending on your server’s operating system. For most Linux distributions, you can use the package manager to install PhalconPHP. For Windows, you can download a DLL file and add it to your PHP extensions directory. After installing, you’ll need to restart your web server for the changes to take effect.

Can I use PhalconPHP with my existing PHP applications?

Yes, PhalconPHP is designed to be as unobtrusive as possible. You can use it alongside your existing PHP code without any issues. This makes it a great choice for gradually refactoring a legacy PHP application.

How does PhalconPHP handle database interactions?

PhalconPHP includes an Object-Relational Mapping (ORM) system that makes it easy to interact with your database. You can use it to create, read, update, and delete records without having to write SQL queries manually. The ORM also supports relationships between tables, making it easy to work with complex data structures.

What kind of applications can I build with PhalconPHP?

PhalconPHP is a versatile framework that can be used to build a wide range of applications. From simple websites to complex web applications, PhalconPHP provides the tools and performance you need. It’s particularly well-suited to high-traffic websites and applications that require real-time interactions.

How do I handle user input with PhalconPHP?

PhalconPHP includes a forms component that makes it easy to handle user input. You can use it to create forms, validate input, and display error messages. The forms component also includes protection against cross-site request forgery (CSRF) attacks.

Does PhalconPHP support MVC architecture?

Yes, PhalconPHP is built around the Model-View-Controller (MVC) architecture. This design pattern separates your application into three interconnected parts, making it easier to maintain and test. PhalconPHP also supports other design patterns, such as Dependency Injection and Event-Driven Programming.

How do I handle errors in PhalconPHP?

PhalconPHP includes a robust error handling system. You can use it to catch and handle exceptions, log errors, and display custom error pages. The error handling system also integrates with the MVC architecture, allowing you to handle errors at the controller level.

Can I use third-party libraries with PhalconPHP?

Yes, PhalconPHP is designed to be extensible. You can use Composer to manage and install third-party libraries. PhalconPHP also includes a loader component that makes it easy to autoload classes from any directory.

How do I secure my PhalconPHP application?

PhalconPHP includes several security features out of the box. These include input filtering, output escaping, and CSRF protection. You can also use the PhalconPHP ACL component to implement access control in your application.

SitePoint SponsorsSitePoint Sponsors
View Author
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week