Friday, August 29, 2008

History, difference betn PHP 4 vs PHP 5

What is PHP?

PHP is a great scripting language that can be used to create amazing forms, features, and functions on your website. PHP is a widely used general-purpose scripting language that is especially suited for web development and can be embedded into HTML. It generally runs on a web server, taking PHP code as its input and creating web pages as output. It can be deployed on most web servers and on almost every operating system and platform free of charge. PHP is installed on more than 20 million websites and 1 million web servers. The most recent major release of PHP was version 5.2.6 on May 1, 2008.

PHP originally stood for Personal Home Page. It began in 1994 as a set of Common Gateway Interface binaries written in the C programming language by the Danish/Greenlandic programmer Rasmus Lerdorf. Lerdorf initially created these Personal Home Page Tools to replace a small set of Perl scripts he had been using to maintain his personal homepage.
Zeev Suraski and Andi Gutmans, two Israeli developers at the Technion IIT, rewrote the parser in 1997 and formed the base of PHP 3, changing the language's name to the recursive initialism PHP: Hypertext Preprocessor.
PHP 4, powered by the Zend Engine 1.0, was released.
PHP 5 included new features such as improved support for object-oriented programming
In 2008, PHP 5 became the only stable version under development. Late static binding has been missing from PHP and will be added in version 5.3.
PHP 6 is under development alongside PHP 5. Major changes include the removal of register_globals, magic quotes, and safe mode.
PHP does not have complete native support for Unicode or multibyte strings; unicode support will be included in PHP 6.


Major Version Minor Version Release date Notes
1.0 1.0.0 1995-06-08 Officially called "Personal Home Page Tools (PHP Tools)". This is the first use of the name "PHP".
2.0 2.0.0 1996-04-16 Considered by its creator as the "fastest and simplest tool" for creating dynamic web pages.
3.0 3.0.0 1998-06-06 Development moves from one person to multiple developers. Zeev Suraski and Andi Gutmans rewrite the base for this version.
4.0 4.0.0 2000-05-22 Added more advanced two-stage parse/execute tag-parsing system called the Zend engine.
4.1.0 2001-12-10 Introduced 'superglobals' ($_GET, $_POST, $_SESSION, etc.)
4.2.0 2002-04-22 Disabled register_globals by default. Data received over the network is not inserted directly into the global namespace anymore, closing possible security holes in applications.
4.3.0 2002-12-27 Introduced the CLI, in addition to the CGI.
4.4.0 2005-07-11 Added man pages for phpize and php-config scripts.
4.4.8 2008-01-03 Several security enhancements and bug fixes. Was to be the end of life release for PHP 4. Security updates only until 2008-08-08, if necessary.
4.4.9 2008-08-07 More security enhancements and bug fixes. The last release of the PHP 4.4 series.
5.0 5.0.0 2004-07-13 Zend Engine II with a new object model.
5.1.0 2005-11-24 Performance improvements with introduction of compiler variables in re-engineered PHP Engine.
5.2.0 2006-11-02 Enabled the filter extension by default.
5.2.6 2008-05-01 Several security enhancements and bug fixes
5.3.0 Mid Oct'08 Namespace support; Improved XML support through use of XMLReader and XMLWriter; SOAP support, Late static bindings, Jump label (limited goto), Closures, Native PHP archives
6.0 6.0.0 No date set Unicode support; removal of ereg extension, 'register_globals', 'magic_quotes' and 'safe_mode'; Alternative PHP Cache; Removal of mime_magic and rewrite of fileinfo() for better MIME support


List are difference between PHP4 and PHP5?



1. Access Specifier (private, public, protected)

- PHP 5 is destined for OOP so (public, private, protected) are accepted.
- PHP 4 safety modes for classes (public, private, protected) are not accepted.

2. Unified Constructors and Destructors

- PHP 4 were functions within the classes bearing the same name as the class.
- PHP 5 introduces a new unified constructor/destructor names.
PHP 5 it is firstly checked if there is a function (method) __construct (). That is, the word construct prefixed by two underscores.
If it does not exist, check if there is a function (method) which has the same name as the class.
Also, the newly added __destruct() (destruct prefixed by two underscores) allows you to write code that will be executed when the object is destroyed.

- Conclusion : Even if you are not aware of the latest news in the domain of PHP5,
your scripts will function without any problem.

/* Code in PHP 4 */
class Item
{
/* class constructor */
function Item() {
}
}

/* Code in PHP 5 */
class Item
{
/* class constructor */
function __construct() {
}
}

3. Abstract Classes

PHP5 lets you declare a class as abstract. 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.

4. Passed by Reference

This is an important change. In PHP4, everything was passed by value, including objects. This has changed in PHP5 -- all objects are now passed by reference.

By default, in PHP 4, creating an instance of or assigning a previously instantiated
object to a new variable will cause a copy of the object to be created.

/* Code in PHP 4 */
$object = new MObject; // a copy of the new instance will be assigned to $object

$object2 = $object; // $object will be copied and then assigned to $object2

$object->property = 1; // changes made to $object are not reflected in $object2

As of PHP 5, to give the programmer more flexibility, references are used by default.
To obtain a copy of an object in PHP 5, you need to clone it.

/* Code in PHP 5 */
$object = new MObject; // a reference will be assigned to $object

$object2 = $object; // assigns the $object reference to $object2

$object->property = 1; // changes made to $object are reflected in $object2

$object3 = clone $object; // $object3 now holds a separate copy of $object

$object3->property = 3; // changes will not be reflected in $object or $object2

The above code fragment was 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.

Note that this also means you can stop using the reference operator (&). It was common practice to pass your objects around using the & operator to get around the annoying pass-by-value functionality in PHP4.

5. Class Constants and Static Methods/Properties

You can now create class constants that act much the same was as define()'ed constants, but are contained within a class definition and accessed with the :: operator.

Static methods and properties are also available. When you declare a class member as static, then it makes that member accessible (through the :: operator) without an instance. (Note this means within methods, the $this variable is not available)

6. The __autoload Function

Using a specially named function, __autoload (there's that double-underscore again!), you can automatically load object files 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.

/* Code in PHP */
function __autoload($class_name) {
require_once "./includes/classes/$class_name.inc.php";
}
Note you can change the autoload function or even add multiple autoload functions using spl_autoload_register and related functions.


7. Error Management

• Classes now support exceptions; the new set_exception_handler() function
allows you to define a script-wide exception handler.

• There is a new error level defined as E_STRICT (value 2048). The E_STRICT error reporting level has been added to the language to emit notices when legacy or deprecated code is encountered. 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.


8. New Extensions

PHP5 also introduces new default extensions.

* SimpleXML for easy processing of XML data
* DOM and XSL extensions are available for a much improved XML-consuming experience. DOMXML, DOMXSL and Sablotron replacement in the form of the libxml2-based DOM and XSL extensions. A breath of fresh air after using DOMXML for PHP4!
* The PHP Data Objects (PDO) for working with databases. An excellent OO interface for interacting with your database. The 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. Hash gives you access to a ton of hash functions if you need more then the usual md5 or sha1.

8. The new PHP 5 object model has many new features which include:

* Encapsulation (visibility)
* Abstract Classes
* Interfaces
* Destructors
* Type Hinting

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.

To add a type hint to a parameter, you specify the name of the class before the $. Beware that when you specify a class name, the type will be satisfied with all of its subclasses as well.

/* Code in PHP */
function echo_user(User $user) {
echo $user->getUsername();
}

9. Databases level

The MySql libraries are no longer bundled with PHP 5. If you are compiling from source you will need to have the libraries installed on your system and use the configure option --with-mysql.

If you are installing PHP 5 on windows, you need to ensure that you have MySql installed and edit the php.ini file to enable the php_mysql.dll extension.

PHP 5 also has a new improved MySql extension called MySqli. For those who don't want to install a separate DBMS PHP 5 provides the SqlLite extension.

10. Other informations (array_merge)

The array_merge() function in PHP 5 now takes only variables of type array as arguments where as in PHP 4 the following code produces this output:

/* Code in PHP 4 */
$non_array = 'item4';
$array = Array('item1','item2','item3');

print_r(array_merge($array, $non_array));

/* Code in PHP 5 */
print_r(array_merge((array) $array, (array) $non_array));
?>

11. Magic Methods

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 3on execution of var_export()’s output.

12. Class Constants and Static Methods/Properties

You can now create class constants that act much the same was as define()’ed constants, but are contained within a class definition and accessed with the :: operator.

Static methods and properties are also available. When you declare a class member as static, then it makes that member accessible (through the :: operator) without an instance. (Note this means within methods, the $this variable is not available)

Conclusion:
* Migrate into PHP 4 to PHP 5 asap
* Gain the advantages of PHP availabilities
* Improve the ability of the Code

2 comments:

Sena said...

Amazing!!! It will definitely usefull for the people who searching php job.

Anonymous said...

duplicate point 5 and 12, cool article, was just googling for difference between 4 and 5

Popular Posts