A Quick view on difference between PHP4 and PHP5
PHP5 has improvements in design, security, and stability as compared to PHP4, there are so many features of OOP added in PHP5 to make it more powerful and useful. If you want more details just go to http://www.php.net and find changes in php.
Object Model/Passed by Reference
This is an important update. In PHP4, everything was passed by value including objects. This has changed in PHP5 all objects are now passed by reference.
$john = new Employee(); $john->age = '22'; $pamella = $john; $pamella->age = '20'; echo $john->age; // Will be '20'
The above code fragment was very common in PHP4.If you needed to duplicate an object; you simply copied it by assigning it to another variable. But in PHP5 you must use the new ‘clone’ keyword.
New Functions/Methods
In PHP5, there are added some of new functions. You can get listing of all of these by PHP Manual.
New Extensions
There are some new default extensions included in PHP5.
PDO for working with databases. It is an Object Oriented interface (layer) for interacting with any type of database.
SimpleXML for easy processing of XML data.
DOM and XSL extensions are available for a improving XML related stuff.
Hash extension gives you a lot of ways (functions) to encrypt data or secure information if you need more then the md5.
Class Const (Constant)
In PHP5, you can create class constants with a keyword const that act same as define()’ed constants, but these should be contained within a class definition and can be accessed with the :: (scope resolution) operator.
Static Methods/Properties
Static methods and properties are also available in PHP5.If you declare a class member as static, then it makes that member/method accessible (through the :: operator) without an instance(object).
Visibility/ Access Control Modifiers
Class methods and properties now have visibility.PHP has 3 levels of visibility
Public makes methods accessible to everyone and properties readable and writable by everyone.
Protected makes members accessible to the class itself and any subclasses of that.
Private makes members only available to the class itself.
Magic Methods
There are a number of “magic methods” that add an assortment to functionality to your classes. Naming of “magic methods” prefixed with a double-underscore.
Some magic methods are __call, __get, __set and __toString.
The __autoload Function
Using a specially named function __autoload() you can automatically load object files(class) when PHP encounters a class that hasn’t been defined yet. Instead of large chunks of include’s at the top of your scripts, you can define a simple autoload function to include them automatically.
function __autoload($class_name) { require_once "./includes/classes/$class_name.inc.php"; }
Constructors and Destructors
In PHP4, a constructor was simply a method that had the same name as the class itself.This caused some problem since if you changed the name of the class, you would have to go through and change every occurrence of that name
But in PHP5, all constructors are named __construct().That is, the word construct prefixed by two underscores.Other then this name change, a constructor works the same way.
Also, __destruct() allows you to write code that will be executed when the object is destroyed.
Abstract Classes
An abstract class cannot itself be instantiated, it is purely used to define a model where other classes extend.You must declare a class abstract if it contains any abstract methods.Any methods marked as abstract must be defined within any classes that extend the class.Note that you can also include full method definitions within an abstract class along with any abstract methods.
PHP5 make you able to declare a class as abstract.
Interfaces
PHP5 introduces interfaces to help in common APIs design.
An interface defines the methods a class must implement. All the methods defined in an interface must be public.
The one big advantage to using interfaces is that a class can implement any number of interfaces. You can extend only a parent class, but can implement an unlimited number of interfaces.
Also, have a look on difference between Abstract class and interface.
Finality
You can now use the final keyword to indicate that a method cannot be overridden by a child. You can also declare an entire class as final which prevents it from having any children at all.
Type Hinting
PHP5 introduces some limited type hinting. This means you can enforce what kinds of variables are passed to functions or class methods. But right now this only work for classes or arrays so no other scalar types like integers or strings.
To add a type hint to a parameter, you specify the name of the class before the $.Be sure that if you specify a class name, the type will be satisfied with all of its subclasses as well.
function echo_user(User $user) { echo $user->getUsername(); }
If the passed parameter is not User (or a subclass of User), then PHP will throw a fatal error.
Exceptions
PHP5 have included exceptions. By using an exception however, you will gain more control, the simple trigger_error notices we were stuck with before.
An exception is just an object. When an error occurs, you throw an exception. When an exception is thrown, the rest of the PHP code following will not be executed. When you are about to perform something “risky or buggy”, surround your code with a try block. If an exception is thrown, then your following catch block is there to catch the error and handle it accordingly. If there is no catch block, a fatal error occurs.
try { $cache->write(); } catch (AccessDeniedException $e) { die('Could not write the cache, access denied.'); } catch (Exception $e) { die('An unknown error occurred: ' .$e->getMessage()); }
E_STRICT Error Level
There is a new error level defined as E_STRICT.It is not included in E_ALL, if you wish to use this new level you must specify it explicitly.E_STRICT will notify you when you use depreciated code.
Foreach Construct and By-Reference Value
The foreach construct now lets you define the ‘value’ as a reference instead of a copy.
foreach($array as $k => &$v) { $v = htmlentities($v); }
Compatibility Issues
The PHP manual has a list of changes that will affect backwards compatibility. Here are some of those.
array_merge() will now give you warnings if any of the parameters are not arrays. In PHP4, you could get away with merging non-arrays with arrays (and the items would just be added if they were say, a string).
As discussed above, objects are now passed by references. If you want to copy a object, make sure to use the clone keyword.
get_*() now return names as they were defined. If a class was called MyClass, then get_class() will return that.
For a detailed view of the Backwards Compatibility Issues, you can also check the PHP Manual, which contains a lot of useful information on this.