Wednesday, March 23, 2011

Differences between PHP 4 and 5

1. In PHP 4 all values are Passed by value where as in PHP 5 all values are passed by reference.
Ex:
$person1 = new Person();
$person1->sex = 'male';
$person2 = $person1;
$person2->sex = 'female';
echo $person1->sex; // Will be 'female'

2. If you want to duplicate the object we should use “Clone” keyword.

3. Access Specifiers have been implemented:
         1. Public is the most visible, making methods accessible to everyone and properties readable and writable by everyone.
         2. Protected makes members accessible to the class itself and any subclasses as well as any parent classes.
         3. Private makes members only available to the class itself.

4. Class constants have been implemented. These are similar to “define” constants but these are contained with in the class only. We can access these variables by using “::” operator.
  
5. Static keyword has been implemented to define static methods with in the class. There is no need to create class object to access static methods of the class. We can use “::” operator.
  
6. Unified constructors and destructors have been added. In PHP4 constructor is a method in class having the name as class name. With any reason if we modify class name we should modify all constructor instances in all over the code. The new constructor method name is “__construct” and new destructor method name is “__destruct”. Destructor will be called when object is destroyed.

7. Abstract classes have been added in PHP5
  
8. PHP5 introduces Interfaces as well to implement better OOP concept.
  
9. Magic methods (Ex: __call, __get, __set, __toString etc) have been added.
  
10. Final keyword and __autoload function are also added.
  
11. Exceptions have been added in PHP5
 
12. Type Hinting is also implemented. This means you can enforce what kind of variables are passed to functions or class methods.
 
13. IN PHP4 “foreach” method works only for arrays where as in PHP5 it will work for both arrays and objects.
 
14. New Extensions: SimpleXML, DOM and XSL, PDO and Hash extensions have been added.
 
15. Lots of extra functions on Arrays, InterBase, iconv, streems, date and time related, strings and other functions have been added. You can find the list of these functions in http://php.net/manual/en/migration5.functions.php URL