Introduction to PHP5 OOPS Features

By | 1st May 2011

PHP5 has made a lot of improvements as regarding OOPS is concerned. Although it has not been up to the mark with the likes of Java, .NET and C++; but when compared to PHP4, PHP5 has made significant improvements.

In this post, I propose to cover the following topics:

  • About PHP5
  • Future of PHP
  • New keywords in PHP5
  • A note on Garbage Collection
  • Naming Conventions
  • Note on E_STRICT
  • Built in Classes (SPL)

 

About PHP5

PHP5 has improved support for OOPS when compared to PHP4. The 4 pillars of OOPS are fully supported by PHP5 i.e. Encapsulation, Inheritance, Polymorphism and Abstraction. You can also create interfaces; if you have been designing classes for a while, you know how important interfaces are.

PHP5 also provides performance improvements as it has made significant changes to its core ‘C’ code. This means that the execution of PHP5 programs will now gain the needed performance improvement.

PHP5 has an integrated support for SOAP as it provides in-built classes for SOAP calls.

PHP5 offers Exception handling mechanism that was missing in its earlier version. You no longer have to write your own logic to return different type of values from one function to another in case of errors. You can now surround your code by try..catch blocks and write necessary logic inside your functions and raise an Exception. You can also extend from the existing Exception class to create your own Exception class.

Future of PHP

PHP6 is in development as of July 2007 and the PHP team is considering to add namespaces support. For java developers, a namespace is an equivalent to packages. With PHP6 a lot of default options in php.ini will either be removed or be enabled. To name a few magic_quotes, safe_mode, register_mode will all be removed.

New keywords in PHP5

  • final
  • interface
  • implements
  • public
  • private
  • protected
  • abstract
  • clone
  • try
  • catch
  • throw
  • this

PHP5 introduces Garbage Collection

For those who don’t know what a Garbage Collection is; here is a note. Garbage Collection refers to the automatic reclamation of dynamically allocated object or variable that are no longer in accessible when an object or variable goes out of scope. An object/variable can goes out of scope only in the following circumstances:

  • when you explicitly unset() it
  • assign null to it
  • when the method/script has completed its execution

Note on E_STRICT

A new error level, E_STRICT, has been introduced and is especially useful in the context of PHP5 OOPS. It is a useful technique for discovering where your code may need upgrading (from PHP4 to PHP5). Error level E_ALL does not encompass E_STRICT and should be set explicitly in php.ini as follows.

error_reporting = E_ALL | E_STRICT

Build in classes (SPL – Standard PHP Library)
PHP5 introduces the Standard PHP Library (SPL), which provides a number of ready-made classes and interfaces. The type of classes PHP5 introduces are for Exceptions, Iterators, Web Services and PHP Data Objects (PDO).

These are ready-made classes that speeds up development. The core functionality behind these classes are written in C because of which it offers significant performance advantages.

This was a note on Introduction to PHP5 OOPS Features. As a continuation to this post, I have planned a complete tutorial on PHP5 OOPS features like classes, interfaces, inheritance, etc. Subscribe below to receive updates as a new tutorial goes live.

Category: PHP

Leave a Reply

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