2

I'm quite used to PHP5 but have to write a PHP4 sync script, now I'm doing some digging to find the differences between PHP5 and 4. Problem is that I get alot of contradiction, some sites tell me that PHP4 has no byref whatever and others tell me this problem only occurs when using foreach..

To clarify an example:

function doSomething()
{
    $aMyAr = array();
    $oUser = new User();

    addUser($aMyAr, $oUser);
}

function addUser($aDestArray, $oUser)
{
    $aMyAr[] = $oUser;
}

I know you will be thinking why don't you just run this script yourself and echo/print_r the output? Well for some reasons PHP4 won't run in the latest WAMP/XAMPP (yes I tried a shitload of apache versions that were said to be compatible...)

Rob
  • 2,466
  • 3
  • 22
  • 40

1 Answers1

3

The change was only that, in PHP4, objects are copied by default, and in PHP5, they're treated as references by default. i.e.:

$a = new stdClass();
$a->prop = "original";
$b = $a;

$b->prop = "changed";
echo $a->prop;

# PHP4 outputs "original" because $a and $b are different objects
# PHP5 outputs "changed" because $a and $b are the same object

When you make a function call or use foreach, in PHP4 the object is copied rather than passed by reference.

To make PHP4 function arguments act like PHP5, you just need to explicitly pass function arguments by reference, i.e.:

function someFunc(& $someObject) {
  $someObject->prop = "changed";
}

someFunc($a);
echo $a->prop; # prints "changed"

So, PHP4 object-oriented code winds up littered with & all over the place (writing true OO code back in the day, this got very tiresome!).

Another example would be assigning by reference. $b =& $a does with objects in PHP4 what the simple $b = $a does in PHP5.

Lastly, there's returning by reference. If you create an object inside a function and want to return it (rather than return a copy of it), you'll have to define the function with &, i.e. function &someFunc() {}.

Again, PHP docs explain the syntax. The best thing to do by far is just not to use PHP4!

joelhardi
  • 11,039
  • 3
  • 32
  • 38
  • Cool, I already had an edit box open with a little extra info for you, in case it helps. Writing and especially debugging PHP4 OO code will get very annoying very fast if you are used to PHP5 (it was annoying enough back then). I would have a hard time going back after so many years of "normal" objects. – joelhardi May 31 '11 at 21:24
  • Yea, I would love to use PHP5.3 the customer can't upgrade since he has alot of webapps running and upgrading would take to much effort to do for this small project. Lucky for me it's only a project with 10 classes so there is not that much that can go horribly wrong. I'm just not using modifiers, __construct, foreach, interfaces and extending. Should be safe right? – Rob May 31 '11 at 21:33
  • Oh yeah, you can definitely do it, I write my share of legacy code too. FYI instead of __construct name the constructor the same as the class, and you can subclass/extend normally. Normal `foreach` will work fine, it's in PHP4 (with arrays of objects, you have to remember to use references if you want PHP5-like behavior, i.e. `foreach ($arr as &$value)`), you just can't use PHP5 Iterators. – joelhardi May 31 '11 at 22:15
  • I use classname as constructor in C# and java aswell so I kinda am used to it anyways :). Thanks for the help. – Rob May 31 '11 at 22:23