logo       

Re: thoughts on php5?: msg#00041

php.tcphp

Subject: Re: thoughts on php5?

If you haven't seen it already--check out the PHP manual on migrating from php4 to php5 at http://us4.php.net/manual/en/migration5.php

zend.ze1_compatibility_mode exists in php5(Zend engine 2) so you can make it behave like php4(Zend engine 1) in regards to handling objects. In a perfect world you want to run php5 with Zend engine 1 compatibility turned off. Unless your application doesn't run properly that is, and then you can enable compatibility in production while you bug hunt in development with it off.

Being php5 passes objects around by reference, and php4 passes by copy you may find some interesting 'features' after switching. Imagine in php4 you assign a changing dataset to an array of objects inside a loop. It works in php4 and prints out x rows in a table as expected. Now you switch to php5 and that same code now only prints a single row, multiple times. The difference is in php4 you were working with a copy of an object for each array element. In php5 its the *same* object for each array element, so you have what appears as an array of objects. But each array element references the same object in php5.

This code should illustrate what I'm trying to say:

<?php
$rObj = new spiffy_object();

while($row = mysql_fetch_array($res, MYSQL_ASSOC)){
$myRows[] = $rObj->load_data($row);
}

echo count($myRows); // say 10 rows, it will echo 10 in php4 & php5

// in php4 this will echo out 10 different rows of data
// in php5 this will echo out a single row of data 10 times
foreach($myRows as $rowObj) {
echo $rowObj->to_HTML();
}
?>

If you want that code to work as it did in php4 two things you can use is enable ze1_compat, or explicitly "clone" the object inside the loop. Inside the while loop below is a method to clone the object for each iteration.

<?php
$rObj = new spiffy_object();

while($row = mysql_fetch_array($res, MYSQL_ASSOC)){
$tmp = clone $rObj;
$myRows[] = $tmp->load_data($row);
}

echo count($myRows); // say 10 rows, it will echo 10 in php4 & php5

// in php4 this will echo out 10 different rows of data
// in php5 this will echo out a single row of data 10 times
foreach($myRows as $rowObj) {
echo $rowObj->to_HTML();
}
?>

Using php5 has been *great* overall. Having exceptions, abstract classes, interfaces, and public, protected, private access for methods and properties is *really* nice! The code is much cleaner and easier to follow.


Erik Anderson wrote:
At work, we've been happily running php-4.3.6 for about a year, and
it's worked great for us. (just for reference, we're running gentoo
linux and apache-2.0.52-r1) Lately, we've been tossing around the
idea of upgrading to php5.

The main driving force behind this is our desire to use mnoGoSearch,
which, if I want to stay within Gentoo's portage system (which I do),
is only possible if we upgrade to php5. Yes, I know, it's always
possible to download and compile php4 with mnoGoSearch support myself,
but I'd much rather stay within portage. Which leads me to my
question:

What are your experiences with upgrading to php5? Have you had any
major issues? Any pitfalls/gotchas to avoid?

Thanks!
-Erik Anderson

---------------------------------------------------------------------
To unsubscribe, e-mail: talk-unsubscribe-4zcLI8jJc/rYtjvyW6yDsg@xxxxxxxxxxxxxxxx
For additional commands, e-mail:
talk-help-4zcLI8jJc/rYtjvyW6yDsg@xxxxxxxxxxxxxxxx


--
D a n i e l J C a i n J r .
Zend Certified Engineer
http://zend.com/zce.php?c=ZEND001685&r=210869656


<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise