TechAlpine – The Technology world

What are the most important features in PHP 5?

PHP 5 features

PHP 5 features

Overview: PHP is a widely used technology in web based application development. PHP 5 is one of the major releases after a long time. PHP 5 introduced a set of new features/functionalities to improve performance, efficiency and many other areas. There are mainly three major areas where the improvement is significant.

In this article I will discuss about the most important features introduced in PHP 5.

Introduction: PHP stands for ‘Hypertext Preprocessor’. It was first introduced by Rasmus Lerdorf in the year 1994. It is a server side scripting language embedded in HTML. It can easily be integrated with commonly used databases e.g. Oracle, mysql, Sybase, Informix, SQLServer etc. PHP supports an array of protocols e.g. POP3, IMAP, LDAP etc. PHP has the following major characteristics –

  • Simplicity
  • Efficiency
  • Security
  • Flexibility
  • Familiarity

Features of PHP 5:

PHP 5 has improved over PHP 4 on 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 in these areas are now used as the significant features. In addition to these, PHP 5 also comes up 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 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

[Code]

< ?php

class MyClass {

function __construct () {

print ” From Parent Class Constructor\n ” ;

}

}

class MySubClass extends MyClass {

function __construct () {

parent::__construct ();

print ” From SubClass constructor\n “;

}

}

?>

[/Code]

  • Destructors – Similar to other object oriented programming language e.g. C++, PHP 5 introduces 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

[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 ();

?>

[/Code]

  • Access modifiers e.g. 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 Language.
  • 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

[Code]

<?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;

}

}

?>

[/Code]

  • Abstract Classes – PHP5 uses the concept of abstract classes in the same way it is being used in other object oriented programming languages.
  • It has support for Static properties and methods.
  • It has support for Final properties and methods.

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

  • New MYSQL Extension – By choice or not, the MYSQL database has become partner of PHP. Almost every PHP developer use MYSQL as their database tool. Still MYSQL extension for PHP presents some challenges. PHP 5 comes up with a refreshed MYSQL extension which offers the following –
    • Prepared Statements
    • Bound Input and Output Parameters
    • SSL Connections
    • Multi Query Functions 

MYSQL takes the advantage of the object oriented approach of PHP 5 which provides an object oriented interface to MYSQL.

  • Interoperable XML tools – PHP 5 addresses the major issues reported for 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 SQLlite 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 the SQLLite database which is embedded in PHP 5 library. PHP 5 comes with SQLLite database which runs smoothly on all PHP 5 installations. It supports the following common DB features –
    • Transactions
    • Sub queries
    • Triggers
  • Better Error Handling and Exception Handling – In PHP 5 a completely new model is conceived called Exception Handling. Having 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 e.g. directory listing, database results or some other components e.g. XML documents. Let us have a look into the following code snippet which implements the directory iterator.

Listing 4: Sample iterator implementation

[Code]

<?php

foreach (new DirectoryIterator($path) as $file) {

print “$file\n”;

}

?>

[/Code]

  • Reflection Classes – In PHP 5 reflection classes are used to examine the classes, methods and to discover object attributes.
  • Better Command line processing – PHP 5 command line version allows individual line processing same as Perl and awk.

Summary: In web development, PHP has emerged as a ‘technology of choice’ in the development community. PHP 5 is one of the major releases after PHP 4. It has enhanced features and functionalities to deal with modern application development needs. Let us summarize our discussion in the form of following bullets –

  • PHP is an acronym for preprocessor hyper text.
  • PHP 5 has lot of enhancements over PHP 4 in the following area –
    • Object Oriented Programming or OOPs
    • MYSQL support
    • XML Support
  • PHP 5 comes with an array of enhanced features. Most important ones are explained above. These can be bulleted into –
    • OOPS Support
    • MYSQL Support
    • XML Support
    • Embedded SQLLite DB
    • Exception Handling
    • SOAP Implementation
    • Reflection Classes
    • Better Command processing

 

Tagged on:

Leave a Reply

Your email address will not be published. Required fields are marked *


1 + = 4

TechAlpine Books
-----------------------------------------------------------