|
Re: thoughts on php5?: msg#00041php.tcphp
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 -- 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> |
|---|---|---|
| Previous by Date: | Re: smarty security problem: 00041, Bob Glamm |
|---|---|
| Next by Date: | Internationalized string sorting: 00041, Bob Glamm |
| Previous by Thread: | thoughts on php5?i: 00041, Erik Anderson |
| Next by Thread: | Re: thoughts on php5?: 00041, Chris Ward |
| Indexes: | [Date] [Thread] [Top] [All Lists] |
| News | FAQ | advertise |