I have a lot of Classes written in PHP 4.3.x. (no 'public' / 'private' definitions, no __constructor/destructor etc.)
I was wondering if PHP 4 Classes work with PHP 5. I know PHP 5 has back-compatibility but I'm not really sure if they all work.
I think PHP 5 handles pass-by-reference values (&$var) differently from PHP 4.
Any answers??In most cases your PHP4 classes will work unchanged under PHP5. And while PHP5 do longer requires the use of & for passing an object by reference, it does not hurt to do so.Thanks for your reply.
What's the exception that you may have to modify your code?? Do you have an example of that?You might want to review this:
<!-- m --><a class="postlink" href="http://www.php.net/zend-engine-2.php">http://www.php.net/zend-engine-2.php</a><!-- m -->
Obviously if your PHP4 class had a method called __construct() then you will have problems.
A more subtle issue is that objects are now always passed by reference. Your code may have depended upon a copy being created. For example:
class Foo
{
public $bar = 1;
}
function process_foo($object)
{
$object->bar = 2;
}
$object = new Foo();
echo "Bar is $object->bar\n";
process_foo($object);
echo "Bar is $object->bar\n";
The code will produce different results under PHP4 and PHP5 because in PHP4, a copy of $object will be created and locally modified in process_foo while in PHP5 the object itself will be modified.In most cases your PHP4 classes will work unchanged under PHP5.
And while PHP5 do longer requires the use of & for passing an object by reference,
it does not hurt to do so.
Is object the only thing passed by reference?
or are primitives also passed by reference?
If so then I suspect that the following code
does not work since $foo is initialized in the
parameter (it's not referenced)?
function foo($foo = "")
{
echo $foo;
}Not sure I understand your question.
Only objects are implicitly passed by reference in PHP5. Other types (arrays, strings) are still passed by copy. However, you can explicitly pass other types by reference using the & operator. No change from PHP4.
I was wondering if PHP 4 Classes work with PHP 5. I know PHP 5 has back-compatibility but I'm not really sure if they all work.
I think PHP 5 handles pass-by-reference values (&$var) differently from PHP 4.
Any answers??In most cases your PHP4 classes will work unchanged under PHP5. And while PHP5 do longer requires the use of & for passing an object by reference, it does not hurt to do so.Thanks for your reply.
What's the exception that you may have to modify your code?? Do you have an example of that?You might want to review this:
<!-- m --><a class="postlink" href="http://www.php.net/zend-engine-2.php">http://www.php.net/zend-engine-2.php</a><!-- m -->
Obviously if your PHP4 class had a method called __construct() then you will have problems.
A more subtle issue is that objects are now always passed by reference. Your code may have depended upon a copy being created. For example:
class Foo
{
public $bar = 1;
}
function process_foo($object)
{
$object->bar = 2;
}
$object = new Foo();
echo "Bar is $object->bar\n";
process_foo($object);
echo "Bar is $object->bar\n";
The code will produce different results under PHP4 and PHP5 because in PHP4, a copy of $object will be created and locally modified in process_foo while in PHP5 the object itself will be modified.In most cases your PHP4 classes will work unchanged under PHP5.
And while PHP5 do longer requires the use of & for passing an object by reference,
it does not hurt to do so.
Is object the only thing passed by reference?
or are primitives also passed by reference?
If so then I suspect that the following code
does not work since $foo is initialized in the
parameter (it's not referenced)?
function foo($foo = "")
{
echo $foo;
}Not sure I understand your question.
Only objects are implicitly passed by reference in PHP5. Other types (arrays, strings) are still passed by copy. However, you can explicitly pass other types by reference using the & operator. No change from PHP4.