Page 1 of 1
php4-->5: backward compatibility
Posted: Thu Jul 27, 2006 1:33 am
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?
Posted: Thu Jul 27, 2006 2:25 am
by daedalus__
I thought that the manual had a pretty decent section about this but I could be wrong, I would check there though.
Posted: Thu Jul 27, 2006 2:35 am
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...
Posted: Thu Jul 27, 2006 3:20 am
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:
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.
Posted: Thu Jul 27, 2006 6:35 am
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).
Posted: Thu Jul 27, 2006 6:54 am
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.
Posted: Thu Jul 27, 2006 7:42 pm
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.
Posted: Fri Jul 28, 2006 12:38 am
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).
Posted: Fri Jul 28, 2006 3:32 am
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
clones the Object $obj but any references within are retained as such iirc.
Posted: Fri Jul 28, 2006 6:24 pm
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.
Posted: Sat Jul 29, 2006 11:10 am
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?
Posted: Sat Jul 29, 2006 11:22 am
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;
}
}
}
}
?>