News

3 PHP frameworks to try now

symfony

Discover 3 of the best PHP frameworks, find which is the perfect fit for you and get started with using them right now.

 

symfony

Symfony

symfony.com
Symfony is a framework based on the Symfony components: these are well known, as they are used in a large selection of products ranging from Drupal to eZ Publish.
Getting started with the product requires the presence of the Symfony Installer: it is a PHP 5.4-based application which generates a project skeleton for you. In most cases, the next act involves the creation of a controller and a route: the first object is responsible for creating a Response object containing data for the client, while the second is responsible for forwarding an incoming URL request.

The Symfony team provides a first-class example illustrating the use of the product’s core features via a lucky number generator:
namespace AppBundleController;
use SensioBundleFrameworkExtraBundleConfigurationRoute;
use SymfonyComponentHttpFoundationResponse;
class LuckyController
{
/**
* @Route(“/lucky/number”)
*/
public function numberAction()
{
$number = rand(0, 100);
return new Response(
‘Lucky number: ‘.$number.’’
);
}

Routes are usually set up using the @Route parameter, which is included via a comment block above the method intended to handle it. The actual invocation returns a Response object containing the HTML or JSON element, which is to be returned to the client. Developers don’t need to create the entire HTML page by hand. Symfony comes with the Twig templating service: if a class is derived from the stock Controller class embedded into Symfony, the templating language can be used with minimal effort:
$html = $this->container->get(‘templating’)->render(
‘lucky/number.html.twig’,
array(‘luckyNumberList’ => $numbersList)
);
return new Response($html);

The developer team itself admits that Symfony isn’t very easy to learn: getting started with the product takes time so factor this into your usage.

Pro

+ Widespread usage
Symfony has a close relationship to well-known projects like Drupal and phpBB.

Con

– Severe learning curve<
Even the developers of the framework admit to difficulty in learning Symfony.
 

 
 

 
Screen shot 2016-05-17 at 11.31.58

Zephir

github.com/phalcon/zephir
Ever since PHP 4 switched to the Zend VM, developers could – in theory – create native extensions to accelerate computationally intensive tasks. Zephir takes this further by providing a set of easy-to-use tools with a C-like language that lets you create extensions easily.

Before extensions can be created, the development environment must be set up on your local workstation. Zephir’s developer team recommends that developers should do the work on Ubuntu. When all components have been downloaded, the Zephir repository must be downloaded and cloned. Finally, a script is to be run that completes the compilation process. After the completion of the set-up process, a new folder structure must be created via this command:
$ zephir init utils

The easiest way to test out Zephir involves the creation of a Hello World example. Let us start out with the following bit of code, which creates a Greeting class located inside the Utils namespace. It contains a static function which returns some value to the shared command/output line:
namespace Utils;
class Greeting
{
public static function say()
{
echo “hello world!”;
}
}

After running zephir build and installing, the module will be embedded into your PHP configuration. Entering php -m should list it by its namespace – if not, something has gone wrong.
In the next step, the simple Hello World class can be called via a file containing PHP code: Zephir is, of course, not limited to static methods. You can also use the language to create complete classes, which behave just like normal PHP code once instantiated.

Note that existing C code cannot be ported to Zephir 1:1 – critical features, eg pointers and pointer arithmetic, are missing from the product. This doesn’t mean that it doesn’t offer the benefits of C. For example, as methods and variables are statically typed, an ‘illegal’ value type will get caught at compile time, thereby preventing hard-to-find runtime bugs.

Zephir is not as widespread as other frameworks; leaving your code in plain PHP means that finding a successor will be easier. On the other hand, developers might want to use Zephir to make themselves more difficult to replace. Another benefit of Zephir is its significantly increased speed – image processing and similar jobs should never be done in vanilla PHP.

Pro

+ Structured language
Static typing means that many error types will be caught by the compiler.

Con

– Requires C knowledge
Be aware that C is a completely different beast to PHP, so will require some studying up.
 

 
 

 
LaravelLogo

Laravel

laravel.com
Laravel provides a prepackaged virtual machine called Laravel Homestead; getting started with the framework is as easy as downloading and spawning the VM. Laravel does not limit itself to providing a stack of tools: it also provides an environment. One interesting example is the deployment of database schemes, accomplished via a migration class:
$table->string(‘name’);
$table->timestamps();
});
}
public function down()
{
Schema::drop(‘tasks’);
}
}

This module can then be run via the artisan migrate command, which applies and/or drops the scheme into the database assigned to the project. Laravel is very popular in the United States: its developers hold meetings regularly and documentation is easy to find.

Pro

+ Common usage
Laravel is popular in the USA – finding developers and conferences to go to is easy.

Con

– It does everything
The immense range of features offered makes getting a grasp of the product difficult.

 

 
 
To find out more about the best PHP frameworks to use today, check out issue 248 of Web Designer!

×