Differences between PHP 4 and PHP 5-VBForums
Results 1 to 1 of 1

Thread: Differences between PHP 4 and PHP 5

  1. #1

    Thread Starter
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Camden, London Mode: PPI
    Posts
    4,904

    Post Differences between PHP 4 and PHP 5

    Differences between PHP 4 & PHP 5

    If you are planning on migrating from PHP 4 to PHP 5, here are the main changes you should be aware of.

    PHP 5 Object Model
    PHP 5 features a greatly improved object model which brings it a lot closer to a fully fledged object orientated programming language. If you have used the PHP 4 object model in any of your applications, here are the major differences between the two.

    Assignments and Object Copying
    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.
    PHP Code:
    /* 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.
    PHP Code:
    /* 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 
    Constructors
    Constructors in PHP 4 are created in the class template as a method of the same name as the class. As of PHP 5 you should use the __construct(), magic function.
    PHP Code:
    /* PHP 4 */

    class Item
    {
        
    /* class constructor */
        
    function Item() {
        }
    }

    class 
    Bin extends Item
    {
        
    /* class constructor */
        
    function Bin() {
            
    parent::Item(); // calling the super class constructor
        
    }

    The same in PHP 5, making it easier to call the super class constructor as you do not need to know its name:
    PHP Code:
    http://www.php.net/manual/en/language.oop5.typehinting.php
    class Item
    {
        
    /* class constructor */
        
    function __construct() {
        }
    }

    class 
    Bin extends Item
    {
        
    /* class constructor */
        
    function __construct() {
            
    parent::__construct(); // calling the super class constructor
        
    }

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


    Databases
    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.

    Other Information
    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:
    PHP Code:
    <?php
        
    /* PHP 4 */
        
    $non_array 'item4';
        
    $array = Array('item1','item2','item3');

        
    print_r(array_merge($array$non_array));
    ?>
    Output:
    Code:
    Array
    (
        [0] => item1
        [1] => item2
        [2] => item3
        [3] => item4
    )
    I PHP 5 the same code would produce an error of type E_WARNING and return null. To fix incompatible code you can typcast the variables which may not be arrays:
    PHP Code:
        // PHP 5
        
    print_r(array_merge((array) $array, (array) $non_array)); 
    A List of backwards incompatible changes between PHP 4 and 5

    Information on Moving to PHP 5

    PHP 5's new Object Model
    Last edited by visualAd; May 9th, 2005 at 05:10 PM.
    Share on Google+

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.