PHPBuilder - Most Important Features in PHP 5



RSS Twitter
Articles

Most Important Features in PHP 5

by: Kaushik Pal
|
September 24, 2014

PHP 5 introduced a set of new features and functionalities to improve performance, efficiency and more. There are three major areas in which the improvement is significant. This article will discuss the most important features introduced in PHP 5.

 

Introduction

PHP stands for 'Hypertext Preprocessor' and was first introduced by Rasmus Lerdorf in 1994. It is a server side scripting language embedded in HTML that can easily be integrated with commonly used databases such as Oracle, MySQL, Sybase, Informix, SQL Server, etc. PHP supports an array of protocols including POP3, IMAP, LDAP, etc. PHP has the following major characteristics:

 

      • Simplicity

      • Efficiency

      • Security

      • Flexibility

      • Familiarity

 

Features of PHP 5

PHP 5 has shown improvement over PHP 4 in the following major areas:

 

      • Object Oriented Programming or OOPs

      • MySQL

      • XML

 

Support for these items has been rewritten completely. The limitations faced in the earlier versions of PHP are now used as the significant features. In addition to these, PHP 5 also arrived with an array of new features, which are discussed below.

 

      • Extensive support for Object Oriented Programming – With PHP 5, developers can take advantages of the following object oriented concepts:

 

         • Constructors – PHP 5 enables developers to have constructors in a class. These constructors are called to create a new instance of the class. In PHP, parent constructors are not called implicitly if the child class has a constructor. In order to run the parent constructor from the child constructor we need to call 'parent::__construct()' from within the child constructor. If there is no constructor in the child class, then it may inherit from the parent class if in the parent class, it is not a private constructor. A sample code implementing constructor is shown below.

 

Listing 1: Sample constructor implementation

< ?php
class MyClass {
   function __construct () {
       print " From Parent Class Constructor\n " ;
   }
}
class MySubClass extends MyClass {
   function __construct () {
       parent::__construct ();
       print " From SubClass constructor\n ";
   }
}
?> 

 

         • Destructors – Similar to other object oriented programming language such as C++, PHP 5 introduced the concept of destructor. This destructor method is called right after it is realized that there are no references to an object. A sample Destructor code is listed below.

 

Listing 2: Sample destructor code

< ? php
class MyClass {
   function __construct () {
       print " Calling from constructor\n ";
       $this->name = " MyClass ";
   }
   function __destruct () {
       print " Destructor Called. Destroying Class ... " . $this->name . "\n ";
   }
}
$obj = new MyClass ();
?> 

 

         • Access modifiers (such as public, protected and private) – In PHP 5, three visibility types (public, private and protected) are introduced. These are used in exactly same way they are used in other object oriented languages.

         • Interfaces – We all know the importance and usage of interfaces in object oriented programming. The following code snippet shows how to declare an interface and how to implement it.

 

Listing 3: Interface and its implementation

<?php
// Declare the interface 'iTemp'
interface iTemp
{
    public function setSchool ( $name, $location );
    public function getHtml ( $template );
}
// Implementing the interface
class myTemplate implements iTemp
{
    private $vars = array ();
    public function setSchool ( $name, $location )
    {
        $this-> locations[$name] = $var;
    }
    public function getHtml ($template)
    {
        foreach($this-> locations as $name => $value) {
            $template = str_replace('{' . $name . '}', $value, $template);
        }
        return $template;
    }
}
?> 

 

         • Abstract Classes – PHP 5 uses the concept of abstract classes in the same way it is being used in other object oriented programming languages.

         • Support for Static properties and methods

         • Support for Final properties and methods

In addition to these, objects are passed by reference in PHP 5 instead of by value. This helps us to get rid of the ampersand character throughout the code.

 

     • New MySQL Extension – Even though it is commonly used, the MySQL extension for PHP previously presented some challenges. PHP 5 includes a refreshed MySQL extension that offers the following:

 

         • Prepared Statements

         • Bound Input and Output Parameters

         • SSL Connections

         • Multi Query Functions

 

MySQL takes the advantage of PHP 5 to provide an object oriented interface.

 

     • Interoperable XML tools – PHP 5 addresses the major issues reported for the PHP 4 XML extension. The new XML extension provided by PHP 5 has the following features:

 

         • It works together as a unified component.

         • These extensions are based on a single XML library.

         • These extensions fully comply with W3 standards.

         • It processes data very efficiently.

 

     • Embedded SQLite database – Even though MySQL is widely accepted and used as the database tool for PHP applications, for smaller applications, it is always advised to use SQLite, which is embedded in PHP 5 library. It supports the following common DB features:

 

         • Transactions

         • Sub queries

         • Triggers

 

     • Better Error Handling and Exception Handling – In PHP 5 a completely new model of exception handling was introduced. With the exception mechanism in place, we can skip the process of checking the return value of every function. Additionally we can separate the business logic from the error handling block.

 

     • Enhanced SOAP Implementation – In the world of web services, SOAP is the key component. The PHP 5 SOAP extension enables developers to create SOAP clients with or without the WSDL file. It also implements the SOAP servers in PHP. In PHP 5 the SOAP extension is developed using the C programming language. The current version of the SOAP extension does not implement the full features of SOAP.

 

     • Iterators – In PHP 5, a new feature, iterator has been added. Using iterator we can use the 'for-each' loop to run through different data structures such as directory listing, database results, or some other components like XML documents. Let us have a look into the following code snippet that implements the directory iterator.

 

Listing 4: Sample iterator implementation

<?php
foreach (new DirectoryIterator($path) as $file) {
    print "$file\n";
}
?> 

 

     • Reflection Classes – In PHP 5, reflection classes are used to examine the classes, methods and to discover object attributes.

     • Better Command line processing – The PHP 5 command line version allows individual line processing, just as Perl and AWK do.

 

Summary

In web development, PHP has emerged as a technology of choice in the development community. PHP 5 has the enhanced features and functionalities to deal with modern application development needs.

 

 

 

About the Author

Kaushik Pal is a technical architect with 15 years of experience in enterprise application and product development. He has expertise in web technologies, architecture/design, java/j2ee, Open source and big data technologies. You can find more of his work at www.techalpine.com and you can email him here.

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Kaushik Pal

Comment:



Comment:

(Maximum characters: 1200). You have characters left.