A little about the Phalcon

I am a long time wanting to post about my use of the framework. Some have already seen, I have presented a talk about the Phalcon in PHP track TDC Porto Alegre this year. And in December in PHP Conference Brazil. But c’mon …

Why use a Framework?

• Use of frameworks has become mandatory in professional development with PHP
• They offer a philosophy and organized structures to easily maintain projects writing less code and making work more fun

How PHP works?


1. PHP have dynamic and weak typing discipline
2. PHP is interpreted (not compiled)
3. Every time a script is loaded, should be interpreted by PHP
4. If a bytecode cache (like APC) isn’t used, syntax checking is performed every time

How traditional php frameworks works?

1. Many files with classes and functions are read at every request made. Disk reading is expensive in terms of performance
2. Modern frameworks use lazy loading technique (autoload) for load and execute only code needed
3. Continuous loading/interpreting could be expensive and impact your application performance
4. When you use a framework most of the code remain the same across development. Why load and interpret it every time?

How a PHP C extension works?

1. C extensions are loaded together with PHP one time, on the web server dispatch process
2. Classes and functions provided by the extension are ready to use for any application
3. The code isn’t interpreted because it is compiled to a specific platform and processor

What is Phalcon?

Phalcon is an open source, full stack framework for PHP 5 written as a C-extension, optimized for high performance. You don’t need to learn or use the C language, since the functionality is exposed as PHP classes ready for you to use. Phalcon also is loosely coupled, allowing you to use its objects as glue components based on the needs of your application.

How Phalcon works?

  1. Components are loosely coupled. You may use the components you want without depending on a full stack
    2. Low-level optimizations provide the lowest overhead for MVC-based applications
    3. Interact with databases with maximum performance by using a C-language ORM for PHP
    4. Phalcon is directly engaged with PHP, so it can directly access internal structures optimizing execution as well

Benchmark Phalcon vs. PHP Conventional frameworks?

The test is done with a clean install of frameworks, with debug turned off and only responding to a request that generates a page that says so: “Hello World”.

Presents from most of tests:

— Yii (YII_DEBUG = false) (yii-1.1.13) — Symphony (2.0.11) — Zend Framework (1:11:11) — Kohana (3.2.0) — FuelPHP (1.2.1) — CakePHP (2.1.3) — Laravel (3.2.5) — CodeIgniter (2.1.0)

Everyone likes benchmarks…
There are many tests, but very many, but I recommend you do your own.
Recently I recorded a hangout on my talk and asked me questions, those that could not answer, like homework, I went in search of answers.

This is the result of independent tests (see systemsarchitect.net) and tests made by the Phalcon crew (see phalconphp.com) and shows what other tests confirm, sometimes not that fast, but in general Phalcon is always by far in all benchmarks.
This awesome increase of speed / request rate is made possibly by pre-compiling.

While HipHop/HHVM (if you never heard, do a google research now) is a real virtual machine inside your server that pre-compiles and serves your PHP, Phalcon is a “c-extension” that will be installed into your operating system (can be installed in Linux, Mac, Windows), not downloaded via Git like usually.

With native PHP, the requested .php file is compiled at every request (which means, PHP, which is itself written in C, will do a syntax check, then the contents will be translated into C, then compiled and then executed).

It does mainly the same thing HHVM does, but without HHVM. Phalcon is the only framework that does not come as pure .php files, it’s delivered as compiled C(++) binaries.
It also comes with a rich set of excellent professional features, so it’s definitly not stripped down!

The code you add will not be precompiled to C bins, so it’s a extra point for HHVM. To be honest, there was a lot of movement in the HHVM world in the last 3 months, and with the upcoming success (I really think so!) and easy implementation of HHVM basically every framework can gain extremely in compiling speed, therefore Phalcon loses it main killer feature a little bit, that’s true…
It would be interesting to see Laravel on HHVM vs. Phalcon benchmarks, but that’s a job for the hardcore experts

From my understanding Phalcon does the syntax-check and the compiling to C only once until the php file changes, so basically a request hits compiled C code, not to-be-compiled PHP code.

Please comment on this article if I’m not correct here (it’s kind of hard to find out how Phalcon works on the inside without being a C developer).

And other Frameworks compiled?

There are other frameworks built as extension, why choose the Phalcon? This is a subtle question. When searching for the fastest framework, you will find the YAF. So why the Phalcon?

Simple, the YAF despite beating the benchmark PHALCON hello world, is not a fullstack. Words, if you need some more sophisticated feature you need to include it, making the advantage of having a framework compiled disappear.

Components

The Phalcon is a full stack framework but nothing was said about MVC architecture. You can work with both the application so that gives you a complete micro MVC and so does not carry anything of the framework and allows you to go loading classes as you are using.

Major components

FrontEnd

  • Volt Template Engine: A templating engine with Master Layouts, Views, Partials and inheritance system.
  • Cache: Self-explanatory, layouts, views and partials are compiled into cache.
  • Tags: The name also delivers the Tags are functions for generating html. Two examples to show the power:
{{ javascript_include(“javascript/jquery.js”) }}

This tag creates the tag based on the url of your application.

{{ form(“posts/save”) }}

This tag opens a form not to the URL posts / save but for the URL that corresponds to the post, save controller action.

BackEnd

— DependencyInjection: As was mentioned, the Phalcon uses only carries what. And that’s responsible for this. — ObjectRelational Mapping: Nothing to write lines of connection and sql. Want to find a user?

User::find(array("email"=>"jacksonfdam@gmail.com"));

— EventManager: This service allows you to perform actions before and / or after each request. Or just for a class, fully customizable.

Comand Line

Nothing like not having to ctrl + c and ctrl + v in a lot of folders and files. This feature lets u create designs, controllers, models, views. All with simple commands.

A new project:

phalcon project myAwesomePhalconProject

A new controller:

phalcon controller Posts

A new model:

phalcon model —table-name Users

The ORM looks for a table in the database with the project settings, maps the attributes and generates the model.

Are there disadvantages?

Always. Just as every framework, it has a learning curve and how it is not very popular, the video lessons and online courses are scarce. In contrast the PHALCON has a very active community on github and a very rich documentation.

The best framework is the one with which it produces more and more fits. If PHALCON is too confusing for you, happens. But if your goal is performance worth taking a look!

Pros and Cons of Phalcon PHP

Incredible right? According to webcoderpro.com, Code Igniter is the most popular framework of 2013. Although it maintains a respectable place on the chart it does not touch Phalcon PHP. So why is Phalcon PHP not the most popular frame? There are a few major drawbacks.

Drawbacks of Phalcon PHP

  1. Compiled Extension (complex install, impossible on shared hosting)
  2. Young compared to competition (March 2012)
  3. If something goes really wrong, you’re debugging C

If I’m honest, as I was writing the drawbacks, I came up with ways around all of them:

Solutions to Drawbacks of Phalcon PHP

  1. It’s actually not that hard to install. Don’t use shared hosting. If you’re at a level in your development where you’re using a big framework and are tackling projects larger than a WordPress site, its probably time to check out a better host like Digital Ocean or Rackspace.
  2. For being just under two years old, the community, documentation and maturity of the framework rival many of the slower frameworks on the graph.
  3. Yes debugging in C for a PHP coder sucks but the community and the help you’ll find on stack overflow will help mitigate this risk.

Let’s sweeten the deal with some pros:

Pros of Phalcon PHP

  1. Fast, blazing fast, really fast, its written in C fast
  2. Traditional MVC code. The PHP part is very similar to other MVC frameworks
  3. Cool factor. It has some of the same coolness of Laravel 4 when it first came out

The Verdict

Phalcon PHP is not perfect for every situation. I’d much rather throw together a quick Laravel instance to mock up a prototype but for a large, performance driven project, hosted on a VPS, I don’t see many reasons to hesitate.