PHP 4 and PHP 5 Comparison - part 1
Which PHP version to use? This is the question that we will try to answer in this article, presenting you with a detailed comparison between PHP 4 and PHP 5.
Table of contents :
PHP, or Hypertext Preprocessor, has been the most used programming language for web pages for more than 8 years now, and for an industry, which is changing every few months, this is a really big achievement. In order to stay on top of things, the
PHP Team is constantly working on improving the code in every way possible.
Currently, there are two PHP versions which are being used,
PHP 4 and
PHP 5. Even though PHP 4 is no longer developed, there are a lot of scripts still using this older version, due to its proven qualities.
At NTC Hosting, we believe in the power of choice. This is why we offer both the no longer developed PHP 4 and the currently supported version PHP 5, with all of our
web hosting plans to form a powerful
php hosting offer. Our clients can easily select the active PHP version for their account at any moment, and then can change it as many times as they want, using our user friendly PHP Settings Tool in the Web Hosting
Control Panel.
And to assist in your choice, we will now take a closer look at the differences between the two most popular
PHP versions.
The first thing that comes to mind when you start to compare PHP 4 and 5 is the:
Object Model
The Object Model was present in PHP 4, but it was completely reworked in PHP 5. Here are some of the most important updates:
- Passed by Reference
This is one of the most important innovations in PHP 5. In PHP 4 everything, including objects, was passed by value. This has been changed in PHP 5 where everything is passed by reference.
PHP Code:
$pObject1 = new Object();
$pObject1->setName('Adam');
$pObject1->setAddress('http://www.talkphp.com/');
$pObject2 = new Object();
$pObject2->setName('Karl');
$pObject2->setAddress('http://www.talkphp.com/');
This is a typical PHP 4 code - if you wanted to duplicate an object, you had to copy it and assign a new value to it. In PHP 5 the coder can simply use the “clone”. This also means that you no longer need to use the reference operator (&) for your code.
Here is how the same code will look in PHP 5 :
$pObject1 = new Object();
$pObject1->setName('Adam');
$pObject1->setAddress('http://www.talkphp.com/');
$pObject2 = clone $pObject1;
$pObject2->setName('Karl');
Since we were chaning only the name, we "cloned" the first object and simply changed the value that needed changing.
- Class Constants and Static Methods/Properties
With PHP 5 you can safely create class constants that act in very much the same way as do defined constants, but are limited within a class definition and can be accessed with “::”. Have in mind that constants must have a constant expression for a value; they can't be equal to a variable or a result of a function call.
Here is how you can define a constant:
PHP Code:
const constant = 'constant value';
And here is how the constant can be accessed in a defined class:
PHP Code:
class MyClass
{
const constant = 'constant value';
function showConstant() {
echo self::constant . "\n";
}
}
The Static Methods and Properties are also a PHP 5 innovation. When a class member is declared as static, it's accessible with "::" without an instance.
- Visibility
In PHP 5 another addition is the “visibility” of the class methods and properties. The visibility has 3 levels:
- Public - the most visible. Methods can be read by everyone and properties can be written or read by anyone.
- Protected - the members are visible only by the actual class they are a part of, as well as by subclasses and parent classes.
- Private - members are visible only to the actual class they are a part of.
Here is an example of how members are declared
<?php
/**
* Define ClassA
*/
class ClassA
{
public $public = 'Public';
protected $protected = 'Protected';
private $private = 'Private';
function printHello()
{
echo $this->public;
echo $this->protected;
echo $this->private;
}
}
$bor = new ClassA();
echo $bor->public; // Will work
echo $bor->protected; // Will give you a fatal error
echo $bor->private; // Will give you a fatal error
$obj->printHello(); // Will display Public, Protected and Private
/**
* Define ClassB
*/
class ClassB extends ClassA
{
// we can redeclare both the public and protected method, but we can't redeclare the private one
protected $protected = 'Protected2';
function printHello()
{
echo $this->public;
echo $this->protected;
echo $this->private;
}
}
$obj2 = new MyClass2();
echo $obj2->public; // Will work
echo $obj2->private; // Is now undefined
echo $obj2->protected; // Will display a fatal error
$obj2->printHello(); // Will show you Public, Protected2, Undefined
?>
- Unified Constructors and Destructors
In PHP 4 the constructor was just a method with the same name as the name of the class. So, if you changed the name of the class, you had to go and update it every time it was used.
In PHP 5, to spare the coders this hassle, the PHP developers have created an unified name for the constructors - "__construct()".
A new addition is the "__destruct()" keyword. When used, the code will be executed only when the object is destroyed.
- Abstract Classes
With PHP 5 you can also create the so called “abstract” classes. These are classes, which are used only as a model to define other classes. If a certain class contains abstract method, it must be defined as abstract.
Here is how a normal class is defined:
class Message{
and here is how an abstract class is defined:
abstract class Message{
- Interfaces
Another new addition in PHP 5 is the “Interfaces”, which can help you design an API. The interface will define the methods, which must be implemented in a class. Have in mind that all methods which are defined in an interface must be public.
A big advantage of this new addition is that in a class you can implement any number of interfaces.
Here is how it all works :
an example of a simple class:
class cow {
function moo() {
echo "moo, moo, moo …";
}
}
and now we implement the interface in the class:
class cow implements animal{
function moo() {
echo "moo, moo, moo …";
}
function breath() { echo "cow is breathing …";}
function eat() { echo "cow is easting …";}
}
When an interface is implemented in a class, the class MUST define all methods and functions of the interface, otherwise the php parser will show a fatal error.
- Magic Methods
All methods, starting with a double underscore ("__") are defined as "Magic Methods". They are set to additional functionality to the classes. It's recommended that you don't use methods with the same naming pattern.
Some of the most used magic methods are: __call, __get, __set and __toString.
- Finality
The "final" keyword has been introduced, so that a method cannot be overridden by a child now. This keyword can also be used to finalize a class in order to prevent it from having children.
- The __autoload function
A very useful function added in PHP 5, which can save the usage of several includes in the begging of the file. The __autoload function will load object files automatically when PHP encounters an undefined yet class.
function __autoload($class_name) {
require_once "./includes/classes/$class_name.inc.php";
}
Proceed to the second part of the PHP4 and PHP5 comparison.