Up and Running with the Fastest PHP Framework on PHP7 in 5 Mins

Share this article

Up and Running with the Fastest PHP Framework on PHP7 in 5 Mins

You may remember our past infatuation with Phalcon, the fastest PHP framework.

In this post, we’ll go through the process of getting it up and running in 5 minutes on one of our Homestead Improved instances. If you’re not interested in why or what Phalcon is, just skip ahead to “Setting it up”.

Phalcon PHP logo

Recap

If you’re not familiar with it, Phalcon is a PHP framework written in Zephir, a language mid-way between C and PHP that helps in developing PHP extensions without having to know C. It’s a golden middle ground which lets PHP developers develop highly optimized modules for their programming language environment without the overhead of learning a completely new language, because Zephir compiles into C, which is what PHP extensions are made of.

Originally, Phalcon was an extension written in pure C – which made it incredibly fast. However, this also introduced massive overhead in fixing bugs or inspecting what’s going on under the hood when something doesn’t work the way it should. It also took ages to add new features, since developing in C is so much more difficult. In version 2, Phalcon was rewritten in Zephir, but as PHP 7 was looming on the horizon and announced a dramatic departure from the extension API of PHP 5+, Zephir could not compile code to PHP 7 compatible extensions and was left behind in PHP 5 land.

We try to walk the bleeding edge here, and there was no more room for Phalcon in our arsenal. With our Homestead Improved box up and running and powering all our tutorials, and with PHP 7’s performance advances making up for any kind of speed gains Phalcon may have offered in PHP 5 time, the cost of installing Phalcon was no longer worth it in any way.

Recently, things have changed: Phalcon went 3.0 LTS.

Apart from following Semver from now on, supporting the 3.0 version for the next 3 years, and various new features and changes you can read about in the post, the biggest update by far is the fact that Zephir now supports PHP 7, and allows for not only the compilation of Phalcon, but also the compilation of any other Zephir code into PHP 7 as well. You can now easily port your custom extensions to Zephir, and have them compiled into PHP 7 in a jiffy. A compiled framework combined with PHP 7’s performance gains offers unparalleled advantages in terms of speed and resource conservation.

Setting it Up

We’ll now go through the process of setting up Phalcon 3 on a PHP 7 powered Homestead Improved instance. You can use any kind of environment you feel comfortable in, but the instructions below will be specific to an Ubuntu 16.04 instance with PHP 7+ installed, and things like Git and Wget present. We’ll also use the Homestead Improved way of defining new sites, via the Homestead.yaml file.

Installing Phalcon

sudo apt-get install software-properties-common
sudo apt-add-repository ppa:phalcon/stable
sudo apt-get update
sudo apt-get install php7.0-phalcon
sudo phpenmod -v 7.0 -s ALL phalcon
sudo service php7.0-fpm restart

The second to last command is a shortcut command to enable a PHP extension. The -v flag tells it which PHP version to activate it for (we want only 7.0 here) and which SAPI (command line, FPM, or both – we want both).

If this tool is not available on your system, then just copy the ini file into the extensions folder like so:

sudo cp /etc/php/7.0/mods-available/phalcon.ini /etc/php/7.0/fpm/conf.d/20-phalcon.ini
sudo cp /etc/php/7.0/mods-available/phalcon.ini /etc/php/7.0/cli/conf.d/20-phalcon.ini

The 20 prefix indicates priority, in case some extensions need to be loaded before others. This is not the case here.

We’ve now unleased the beast. Looking at phpinfo(), it’s there.

Phalcon is active in phpinfo

Demo Phalcon App

We can test things on a demo Phalcon application – the finished Invo app.

Configuring Nginx for Phalcon

We need to add our app into the Sites block of Homestead.yaml:

    - map: phalcon-tut.app
      to: /home/vagrant/Code/phalcon-tut/public

Make sure phalcon-tut.app or what ever vhost name you choose is added to your host OS /etc/hosts file in order to resolve to the VM’s IP address.

Of course, we also need to run vagrant provision outside the VM in order to apply this Nginx setup.

Finally, we edit Nginx’s location block, and change from

location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

to

location / {
        try_files $uri $uri/ /index.php?_url=$uri&$args;
    }

We need to restart Nginx to apply the changes:

sudo service nginx restart

This takes care of the server end of things.

Bootstrapping the app

cd /home/vagrant/Code
git clone https://github.com/phalcon/invo phalcon-tut

Once the app has been cloned, we need to initialize its database, as per instructions. We’ll update those to be a bit more modern, by using utf8mb4 instead of utf8.

echo 'CREATE DATABASE invo CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci' | mysql -u homestead -psecret
cat schemas/invo.sql | mysql -u homestead -psecret invo

There’s one more step which isn’t described in the app’s repo – changing the baseUri in the configuration. We have to go into app/config/config.ini and change baseUri to /, otherwise assets (JS/CSS) will fail to load.

After doing that, sure enough, INVO is up and running.

Invo screenshot

Conclusion

In this tutorial, we quickly configured Phalcon to run on Homestead Improved and PHP 7, and got a demo app up and running. With installation instructions significantly simplified compared to what they were, and with ready-made apps to test it out thoroughly, why not go for a Phalcon test run and tell us what you think?

Now that Phalcon is available on PHP 7, will you be using it for any of your new projects? How do you feel about Zephir – would you use it to close-source or hyper-optimize any of your microservices / modules, or do you still feel like that’s too much overhead?

Frequently Asked Questions about PHP7 and the Fastest PHP Framework

What makes PHP7 faster than its previous versions?

PHP7 is significantly faster than its predecessors due to several improvements. The most notable one is the introduction of the Zend Engine 3.0, which offers improved performance, reduced memory consumption, and better error handling. PHP7 also includes new operators and language features that streamline coding and reduce the need for unnecessary commands, thus speeding up the execution time.

How does PHP7 improve error handling?

PHP7 introduces a new object-oriented approach to error handling with the introduction of Throwable errors. This feature allows developers to catch both exceptions and errors using a single block of code, making it easier to manage and handle errors in the application.

What are the key features of the fastest PHP framework?

The fastest PHP framework, Phalcon, is built as a C-extension, meaning it’s pre-compiled and offers high performance. Key features include low memory consumption, high execution speed, and a rich set of features like ORM, caching, and auto-loading. It also supports MVC and HMVC, template engines, form builders, and various other functionalities.

How does Phalcon compare to other PHP frameworks?

Phalcon stands out from other PHP frameworks due to its speed and low resource usage. Being a C-extension, it doesn’t need to be interpreted like PHP, resulting in faster execution. It also offers a comprehensive set of features, making it a versatile choice for many web development projects.

What are the benefits of using PHP7 for web development?

PHP7 offers several benefits for web development. It’s faster, uses less memory, and provides better error handling. It also supports new language features that streamline coding, making it easier to write and maintain. PHP7 also offers better support for modern web development practices and technologies.

How can I get started with PHP7 and Phalcon?

To get started with PHP7 and Phalcon, you need to install PHP7 on your server and then install the Phalcon extension. Once installed, you can start building applications using Phalcon’s rich set of features. There are also many resources and tutorials available online to help you get started.

What are the system requirements for PHP7 and Phalcon?

PHP7 requires a server running Windows, macOS, or Linux, with at least 128MB of RAM (though more is recommended). Phalcon requires PHP7 or later and the Phalcon extension installed on your server.

How does PHP7 handle type declarations?

PHP7 introduces type declarations for both function arguments and return types. This feature allows developers to specify the expected type of data, making the code easier to read and debug.

What is the future of PHP7 and Phalcon?

PHP7 continues to be updated with new features and improvements, ensuring its relevance in modern web development. Phalcon also continues to evolve, with ongoing development focused on improving performance, adding features, and ensuring compatibility with the latest versions of PHP.

Are there any drawbacks to using PHP7 and Phalcon?

While PHP7 and Phalcon offer many benefits, they also have some drawbacks. For PHP7, the upgrade from previous versions may require some code refactoring. For Phalcon, being a C-extension, it can be more difficult to install and use than other PHP frameworks, especially for beginners.

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.

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