Laravel And PHP

PHP gained a bad reputation over the years due to lots of badly-written websites and web applications, and its shortcomings when compared to other, more mature languages. PHP is also notorious for its naming inconsistencies and questionable design decisions regarding its syntax. As a consequence, there has been an exodus to more credible frameworks written in Ruby and Python. Since these languages were nowhere as feature-rich for the Web as PHP, the creators of Ruby on Rails and Django, for instance, had to recreate some essential building blocks, such as classes, to represent HTTP requests and responses and were, therefore, able to avoid some of the mistakes that PHP had made before them, due to the luxury of starting from a blank slate. These frameworks also forced the developer to adhere to a predefined application architecture.

However, it’s now a great time to discover (or fall back in love with) PHP again, as over the past couple of years the language has rapidly evolved to include new features such as closures and traits, and a de facto package manager in Composer. Past complaints of PHP when compared to other languages are now exactly that, of the past, and PHP is slowly but surely changing the bad reputation it has suffered from, for so long.


A more robust HTTP foundation

After years of people developing their own, unique approach of handling common tasks, such as handling requests and responses, specifically for their own projects, one framework took a different approach and instead, began creating components that could be used in any codebase no matter its foundation, be it homegrown or based on a framework. The Symfony project adopted these principles to recreate a more solid, flexible, and testable HTTP foundation for PHP applications. Along with the latest version of Drupal and phpBB, Laravel is one of the many open source projects that use this foundation together with several other components that form the Symfony framework.

Recommended :  Useful tips for Collection operations In Laravel

Laravel is such a project that relies on the HTTP foundation created by Symfony. It also relies on other components created by Symfony, as well as a variety of other popular libraries, such as SwiftMailerfor more straightforward e-mailing, Carbon for more expressive date and time handling, Doctrine for its inflector and database abstraction tools, and a handful of other tools to handle logging, class loading, and error reporting. Instead of re-inventing the wheel, Laravel decided to hop on the shoulder of giants and embrace these pre-existing mature components.


Embracing PHP

One way in which Laravel differs from its contemporaries is that it openly embraces new features of PHP and in turn requires a fairly recent version (at least 5.4). Previously, other frameworks would build support for older versions of PHP to maintain backwards-compatibility for as long as possible. However, this approach meant that those same frameworks couldn’t take advantage of new features in the newer versions of PHP, in turn, hampering the evolution of PHP. Using Laravel 5, you will get to grips with some of the newer features of PHP. If you’re new to PHP, or coming back to the language after a while, then here’s what you can expect to find:

  • Namespaces: More mature languages such as Java and C# have namespaces. Namespaces help developers avoid naming collisions that might happen if say, two different libraries have the same function or class name. In PHP, namespaces are separated by backslashes, which is usually mirrored by the directory structure, with the only difference being the use of slashes on Unix systems, in accordance with the PSR-4 convention. A namespace, such as <?php namespace Illuminate\Database\Eloquent is declared at the top of the file. To use code from another namespace, it needs to be imported, which can be done with the use keyword, and then by specifying the namespace, that is, use Illuminate\Database\Eloquent\Model. Another advantage of namespaces is that you can alias imported classes, so as to avoid collisions with classes with the same name in another namespace or the global namespace. To do this, you use the as keyword after the use statement as use Foo\Logger as FooLogger;
  • Interfaces: Interfaces specify the methods that a class should provide when that interface isimplemented. Interfaces do not contain any implementation details themselves, merely the methods (and the arguments those methods should take). For instance, if a class implements Laravel’sJsonableInterface instance, then that class will also need to have a toJson() method. Within Laravel, interfaces tend to be referred to as Contracts.
  • Anonymous functions: These are also known as closures and were introduced in PHP 5.3. Somewhat reminiscent of JavaScript, they help you to produce shorter code, and you will use them extensively when building Laravel applications to define routes, events, filters, and in many other instances. This is an example of an anonymous function attached to a route: Route::get('/', function() { return 'Hello, world.'; });.In Laravel, this code creates a new routewhen the base path of a website is requested. When it is, the code in the closure is executed and returned as the response.
  • Overloading: Also called dynamic methods, they allow you to call methods such aswhereUsernameOrEmail($name, $email) that were not explicitly defined in a class. These calls get handled by the __call() method in the class, which then tries to parse the name to execute one or more known methods. In this case, ->where('username', $username)->orWhere('email', $email).
  • Shorter array syntax: PHP 5.4 introduced the shorter array syntax. Instead of writingarray('primes' =>array(1,3,5,7)), it is now possible to use just square brackets to denote an array, that is, ['primes'=>[1,3,5,7]]. You might know syntax if you’ve used arrays in JavaScript.
Recommended :  What are collections in laravel


laravel newbie ? then read this introduction to laravel

About the author

Deven Rathore

I'm Deven Rathore, a multidisciplinary & self-taught designer with 3 years of experience. I'm passionate about technology, music, coffee, traveling and everything visually stimulating. Constantly learning and experiencing new things.

Pin It on Pinterest