php4-->5: backward compatibility
Moderator: General Moderators
- julian_lp
- Forum Contributor
- Posts: 121
- Joined: Sun Jul 09, 2006 1:00 am
- Location: la plata - argentina
php4-->5: backward compatibility
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?
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?
- daedalus__
- DevNet Resident
- Posts: 1925
- Joined: Thu Feb 09, 2006 4:52 pm
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
I've only had issues with 3rd party code, never my own. And those issues stemmed from poor coding.julian_lp wrote:Yes, I'll check there, but I'm interested in personal experiences about the subject as well...Daedalus- wrote:I thought that the manual had a pretty decent section about this but I could be wrong, I would check there though.
An example would be PHPDocumentor in older releases was trying the do:
Code: Select all
$this = $foo;For the most part, they're backward compatible.
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
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:
However, if the object contains any references, they too are cloned, which can be a PITA at times.
For compatability on both php4 and 5, use:
Code: Select all
function cloneObject ($obj)
{
return unserialize(serialize($obj));
}- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
The problem you have there is recursion, and how 'deep' should it go etc.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.
In PHP5 cloning is not a problem
Code: Select all
$clone = clone $obj;- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
I meant, for scenarios such as:
How does it know to clone model and db? How would you clone those within the classes?
Code: Select all
$db = new DB;
$model = new Model($db);
$view = new View($model);
$view2 = $view->clone();- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
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;
}
}
}
}
?>