php4-->5: backward compatibility

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
julian_lp
Forum Contributor
Posts: 121
Joined: Sun Jul 09, 2006 1:00 am
Location: la plata - argentina

php4-->5: backward compatibility

Post by julian_lp »

I've just installed php5 in my local server, beacause it certainly has a cleaner oop model than version 4.
My main concern now, is be aware of backward compatibilities problems with php4 code.
From your personal experiences, what kind of code have more chances to be broken? What would you say I have to change in the former code?
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

I thought that the manual had a pretty decent section about this but I could be wrong, I would check there though.
User avatar
julian_lp
Forum Contributor
Posts: 121
Joined: Sun Jul 09, 2006 1:00 am
Location: la plata - argentina

Post by julian_lp »

Daedalus- wrote:I thought that the manual had a pretty decent section about this but I could be wrong, I would check there though.
Yes, I'll check there, but I'm interested in personal experiences about the subject as well...
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

julian_lp wrote:
Daedalus- wrote:I thought that the manual had a pretty decent section about this but I could be wrong, I would check there though.
Yes, I'll check there, but I'm interested in personal experiences about the subject as well...
I've only had issues with 3rd party code, never my own. And those issues stemmed from poor coding.

An example would be PHPDocumentor in older releases was trying the do:

Code: Select all

$this = $foo;
That's crazy, that's like saying "I am now mickey mouse" when really you're Bugs Bunny.

For the most part, they're backward compatible.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Transition from PHP 4 to PHP 5 was extremely smooth for me, once SimpleTest got its reflection library updated to work with PHP 5. The only gotcha I still have to work around is references versus copying (although most of the time it really doesn't matter).
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

The only problem I have seen with the transition, again with reference vs copying, was when you actually need to clone an object, rather than reference it.

For compatability on both php4 and 5, use:

Code: Select all

function cloneObject ($obj)
{
    return unserialize(serialize($obj));
}
However, if the object contains any references, they too are cloned, which can be a PITA at times.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

I would rather implement clones using a ->clone() member function which can implement your unserialize(serialize()) hack but also be implemented so that if you've got silly references, you can hand specify which member variables should be copied and which should have their references maintained.
Ward
Forum Commoner
Posts: 74
Joined: Thu Jul 13, 2006 10:01 am

Post by Ward »

From my experience, pretty much everything that works in 4 will work in 5, but not vice-versa. There are minor differences in certain functions, where the return value may be different, especially when returning false (or 0, or -1, etc).
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Ambush Commander wrote:I would rather implement clones using a ->clone() member function which can implement your unserialize(serialize()) hack but also be implemented so that if you've got silly references, you can hand specify which member variables should be copied and which should have their references maintained.
The problem you have there is recursion, and how 'deep' should it go etc.

In PHP5 cloning is not a problem

Code: Select all

$clone = clone $obj;
clones the Object $obj but any references within are retained as such iirc.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

The problem you have there is recursion, and how 'deep' should it go etc.
Implement clone on all objects that could possibly need to be cloned.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

I meant, for scenarios such as:

Code: Select all

$db = new DB;
$model = new Model($db);
$view = new View($model);

$view2 = $view->clone();
How does it know to clone model and db? How would you clone those within the classes?
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Code: Select all

<?php

class DB
{
    // regular DB stuff
    function &clone() {
        // always return a reference to the DB object,
        // never create a new connection
        return $this;
    }
}

class Model
{
    // regular Model stuff
    
    var $db;
    
    function Model(&$db) {
        $this->db =& $db;
    }
    
    function &clone() {
        // I would assume we'd only want one instance
        // of the model floating around
        return $this;
    }
}

class NullModel extends Model
{
    // place-holder model
    function NullModel() {}
}

class View
{
    // regular View stuff
    
    function View(&$model) {
        $this->model =& $model;
    }
    
    var $model;
    
    function clone() {
        // reflection used here, you might want to hard-code
        $cloned_view = new View(new NullModel());
        $class_vars = get_class_vars('View');
        foreach ($class_vars as $name => $value) {
            if (is_object($this->$name)) {
                $cloned_view->$name = $this->$name->clone();
            } else {
                $cloned_view->$name = $this->$name;
            }
        }
    }
}

?>
Post Reply