Search This Blog

Thursday, February 25, 2010

20 new features in PHP5

1. public/private/protected access modifiers for methods and properties before it was using var
2. Unified constructor name __construct() and destructor __destruct() (__[function name ] are mentioned as magic function user should not declare or define a function with __ if they really know the meaning and there is a real need for that )
3. Interfaces  -- this is like class which can have any no of functions and variable but we can only declare not define here and all these thing should be public , All the methods on the interface should be defined on the child class else it will show an fata (Now Multiple inhertance is possible with this )
5. instanceof operator -- before it was is_a() which will check wheather a function is a of an particular object like that
6. final methods -- stops overloading functions
7. final class -- stops class inhertance
8. Explicit object cloning -- we can define __clone method to be called automatically after cloning an object
9. Class constants -- we can have constants inside a class and use object of that class to refer that constant

10.Static methods -- these methods can be called without object using class name like class1::method1 where method 1 is a static method on class1
eg: class1::method1
11. Static members : Classes definitions can now include static members (properties), accessible via the class. Common usage of static members is in the Singleton pattern.

12. abstract classes
A class may be declared as abstract so as to prevent it from being instantiated. However, you may inherit from an abstract class
13. abstract methods
A method may be declared as abstract, thereby deferring its definition to an inheriting class. A class that includes abstract methods must be declared as abstract.
14. Class type hints
Function declarations may include class type hints for their parameters. If the functions are called with an incorrect class type an error occurs.
eg:
class class1(classname obj)
{
...
}
15. Support for dereferencing objects which are returned from methods.
In PHP 4, you could not directly dereference objects which are returned from methods. You would have to first assign the object to a dummy variable and then dereference it.
eg :
PHP4
$dummy = $obj->method(); //this method returns an object which is assigned to new variable then calling the menthod of the returned object on the second statement
$dummy->method2();
PHP5
$obj->method()->method2(); // now we can call directly

16. Iterators
PHP 5 allows both PHP classes and PHP extension classes to implement an Iterator interface. Once you implement this interface you will be able to iterate instances of the class by using the foreach() language construct.

17. __autoload() -- include the file inc or php when it is required using the class name
eg:
function __autoload()
{
include_once ($class_name.".php");
}

18. Exception handling : try/throw/catch

19. foreach with references
eg: foreach ($var as &$key=>$value){...}
20. default values for by-reference parameters
eg: function method1(&$arg1=10){...}

No comments:

Post a Comment