Object Oriented Programming
 
Difference between PHP4 & PHP5
 
In general when a new software version appears everybody hurries to update it especially if it is for free. PHP 5 has just appeared and it seems that there are people who use the PHP 4 version and others who use the PHP 5 one.
 
You probably know that PHP 5 is destined for OOP, but it appears that habitual programming can be used too. Moreover, OOP is used in PHP4 as well, with the difference that in PHP5 things are a little more evaluated.
 
This means that in PHP4 safety modes for classes (public, private) are not accepted. In PHP4 the objects are a kind of structures which accept objects and functions as well, according to OOP. In PHP4 they are useful as well.
 
If you are used to working with PHP4 you can use PHP5 with no problems because the differences are not significant and the changes were made so that programmers would not be confused. An example would be class builders which, in PHP4 were functions within the classes bearing the same name as the class.
 
In PHP5 it is firstly checked if there is a function (method) __construct (). If it does not exist, check if there is a function (method) which has the same name as the class. This means that even if you are not aware of the latest news in the domain of PHP5, your scripts will function without any problem.
 
But it is strongly recommend to migrate to PHP5 (if you are using PHP4) or to start with PHP5 if you are new to PHP, because PHP has discontinued the support of PHP4 and it will not release any patches or security updates for PHP4. Since PHP has withdrawn support for PHP4 web servers will discontinue support of PHP4 sooner or later, so the sooner you change the better for you.
 
Differences Between PHP 4 and 5
 
Language Features PHP 5 allows limited type hinting. This allows you to specify that the parameter to a function or class method can only be of a specific class (or one of its subclasses), or an array. However, you may not specify any other scalar types.
 
The foreach construct now supports by-reference declaration of the value element. A number of new functions, particularly for string and array manipulation, has also been added to the core platform.
 
Objects For all intents and purposes, all objects in PHP 5 are passed by reference. This means that assigning an object to a variable will not create a copy of the former, but simply creates another reference to it. Constants, as well as static methods and properties, can now be defined within the scope of a class.
 
Class methods and properties now feature visibility, and can be declared as public, private or protected. Classes and methods can also be declared as final to prevent further inheritance. Since all objects are assigned by reference, you now need a specialized mechanism to copy objects. This is provided by the clone construct and the __clone() magic method.
 
PHP 5 features unified constructors and destructors-all constructors should now be named __construct(), and the new __destruct() magic method has been added for object destruction.
 
With the addition of interfaces and abstract classes, PHP developers now have far greater control over how they implement their object-oriented code. Interfaces can be used to define common APIs, while abstract classes provide models for class implementations that follow a specific blueprint.
 
Class definitions can now be loaded on demand by using the __autoload() function.
 
MagicMethods A multitude of new "magic" methods has been introduced in PHP 5: __get() and __set() are called when accessing or assigning an undefined object
 
property, while __call() is executed when calling a non-existent method of a class. __isset() is called when passing an undefined property to the isset() construct.
 
. __unset() is called when passing an undefined property to unset().
. __toString() is called when trying to directly echo or print() an object.
. __set_state() is inserted dynamically by var_export() to allow for reinitialization on execution of var_export()'s output.
 
Selected New Extensions
 
. SimpleXML allows easy access to XML data using object and array notation.
 
. PHP 5 also introduces a DOMXML, DOMXSL and Sablotron replacement in the form of the libxml2-based DOM and XSL extensions.
 
. The PHP Data Objects (PDO) extension provides a unified database access extension that allows access to many different types of database systems by using a common interface. PDO is not an abstraction layer-except for prepared queries, it does nothing to abstract the actual database code (SQL), itself.
 
. The hash extension is a new replacement for the GPLed libmhash; it was added to the PHP core starting with version 5.1.2. It can produce hashes using many algorithms, including the familiar MD5 and SHA1, as well as some more secure (albeit slower) algorithms, such as snefru.
 
. The Standard PHP Library (SPL) provides numerous interfaces that enhance the way classes interact with the PHP language, including the new Iterator interfaces.
 
. The new Reflection extension allows for runtime introspection of executing PHP code.
 
ErrorManagement
 
Classes now support exceptions; the new set_exception_handler() functionallows you to define a script-wide exception handler.
 
. The E_STRICT error reporting level has been added to the language to emit notices when legacy or deprecated code is encountered.