php4, php5, objects and cloning

Posted by David N. Welton Tue, 27 Feb 2007 09:55:00 GMT

We take a very simple class:

class Foobar {
var $myvar = 1;

function Foobar() {
}

function printit() {
    echo "var is " . $this->myvar . "\n";
}

function incr() {
    $this->myvar += 1;
}

}

$f = new Foobar();
$a = $f;
$b = $f;

$a->printit();
$a->incr();

$b->printit();

And run it with PHP4 and PHP5:

davidw@byron:/tmp$ php4 foobar.php 
var is 1
var is 1
davidw@byron:/tmp$ php5 foobar.php 
var is 1
var is 2

Apparently it's possible to use the clone function to do what we want with PHP5, but... that means we have to track down every single place in the code where there is something like $foo = $bar. And hope that we got them all, because otherwise subtle bad things might happen. The codebase I've inherited isn't great to begin with, and hunting down every object copy is bound to be torture. Ouch.

Update

It's been pointed out that my description of exactly what's happening internally isn't correct. However, from my point of view, the end result is the same: a massive search and replace operation in order to obtain the "old" behavior by using clone. Most likely, this means that we won't be able to upgrade soon, which means that we won't be able to take advantage of some of the nice new PHP5 features, which will slow down other works in progress. For want of a nail...

10 comments | atom

Trackbacks

Use the following link to trackback from your own site:
http://journal.dedasys.com/trackbacks?article_id=php4-php5-objects-and-cloning&day=27&month=02&year=2007

Comments

Leave a response

  1. Pierre
    29 minutes later:

    There is a major change in how object are treated in assigned in php5, and this is a good thing :)

    To get a more detail explanation about references in php (what you do here is about reference not cloning), check:

    http://blog.libssh2.org/index.php?/archives/51-Youre-being-lied-to..html#extended

    To really clone an object use "clone", for example (using the same class):

    $f = new Foobar(); $a = $f; $b = $f;

    $a->printit(); $a->incr(); $b->printit(); $b->incr(); $a->printit();

    echo "clone:\n"; $f = new Foobar(); $a = clone $f; $b = clone $f;

    $a->printit(); $a->incr(); $b->printit(); $b->incr(); $a->printit();

  2. Pierre
    29 minutes later:

    Oops, looks horrible without new lines :-/

  3. Asbjørn Ulsberg
    about 15 hours later:

    Face it: PHP sucks. PHP5 sucks less than PHP4, but it still blows in my opinion. Ruby has proven that you don't need a chronically badly designed language and platform to make it good for prototyping and easy for beginners, and at the same time not overly complex to be able to model beautiful and intuitive libraries and frameworks.

  4. David N. Welton
    about 16 hours later:

    Sorry about the formatting Pierre. Sooner or later I'll upgrade this thing. Asbjørn, I'm certainly not wild about PHP either, and this doesn't improve my mood.

  5. Pierre
    about 17 hours later:

    Hi David,

    No problem, if you can make the link clickable as well, it may help other readers.

    @Asbjørn

    Sorry, I'm too stupid to understand the pertinence of your comment. I never needed a specific language to design something good. Can we not try to move a step forward and stop this stupid FUD about "putyourfavavouritelanguagehere" sucks les s than "putyourworstnightmarehere"?

  6. David N. Welton
    about 17 hours later:

    Pierre, I'll see if I can fiddle with the comment later to get the link working.

    Having written a language of my own, I agree that being too harsh is disrespectful and belittles the hard work of others. However, I also think that there are advantages to some languages over others. How would you like to write a dynamic web site in Forth?:-) Of course, it's certainly a subjective topic, because in the end, languages are made for people, not computers, but... still, I think that there are advantages (at times large ones) in using certain languages. That's how PHP became popular, right? It was better than the alternatives for making dynamic web sites at the time.

  7. Jon
    about 19 hours later:

    I remember a long, long time ago, the design documents for PHP5/Zend basically stating that they were going to totally break existing code that relied on objects on PHP4, but that it was such a mess, that anyone crazy enough to use classes in PHP4 deserved it.

  8. Pierre
    1 day later:

    "How would you like to write a dynamic web site in Forth?:-) "

    I remember one having wrote bash-cgi or something like that :-)

    My point was also about good design is not necessary dependent of the language. I see horrible java code as well as beautifull PHP code (yes, that exists ;-).

    "That's how PHP became popular, right? It was better than the alternatives for making dynamic web sites at the time"

    Exactly, and we all learn from other projects experience, from the good as well as the bad. PHP is learning from Ruby (at least the php users), that's part of the OS magic :-)

  9. Hopson
    1 day later:

    For the sake of getting your stuff working, you might be able to toggle the value of:

    http://php.net/manual/en/ini.core.php#ini.zend.ze1-compatibility-mode

    Mixing PHP4/5 semantics like that might be more confusing that it's worth, though.

  10. Hopson
    1 day later:

    Rats, I should have quoted that URL; "#ini.zend.ze1-compatability-mode" is part of the link.

Leave a comment