|
Posted by FFMG on 11/19/07 08:15
Hi,
I am slowly moving my code to php5.
But I would like to make it backward compatible in case something bad
happens, (and to make sure I understand what the changes are).
The way the constructors work seem to have changed quite a bit and I am
not getting the same behavior across the versions.
// Some simple code/
<?php
class TestClass
{
function TestClass() // For php4
{
$this->__construct();
}
var $_classValue = '';
function __construct()// For php5 && 4
{
global $globalValue;
$globalValue = $this;
// I use a random to make certain we are talking about the same
class.
$this->_classValue = md5(uniqid(mt_rand()));
}
function __destruct()
{
}
function setObject( )
{
global $globalValue;
$globalValue = $this;
}
}
global $globalValue;
$globalValue = null;
$testClass = new TestClass();
var_dump( $globalValue );
echo "<br />----- <br /><br />\n";
$testClass->setObject();
var_dump( $globalValue );
echo "<br />----- <br /><br />\n";
var_dump( $testClass );
echo "<br />----- <br /><br />\n";
?>
// ------------------------
The output in php5 is, (I think as expected).
object(TestClass)#1 (1) { ["_classValue"]=> string(32)
"352f867d5651b612ea3448c7a753e2cc" }
-----
object(TestClass)#1 (1) { ["_classValue"]=> string(32)
"352f867d5651b612ea3448c7a753e2cc" }
-----
object(TestClass)#1 (1) { ["_classValue"]=> string(32)
"352f867d5651b612ea3448c7a753e2cc" }
-----
But the output in php4 is not correct.
It is as if the class is not yet constructed.
object(testclass)(1) { ["_classValue"]=> string(0) "" }
-----
object(testclass)(1) { ["_classValue"]=> string(32)
"53b6f8088bd3c099e5b2b29e4f1361b2" }
-----
object(testclass)(1) { ["_classValue"]=> string(32)
"53b6f8088bd3c099e5b2b29e4f1361b2" }
-----
How would you fix the code so that i can set $globalValue as a
reference of $testClass in the constructor?
Thanks
FFMG
--
'webmaster forum' (http://www.httppoint.com) | 'Free Blogs'
(http://www.journalhome.com/) | 'webmaster Directory'
(http://www.webhostshunter.com/)
'Recreation Vehicle insurance'
(http://www.insurance-owl.com/other/car_rec.php) | 'Free URL
redirection service' (http://urlkick.com/)
------------------------------------------------------------------------
FFMG's Profile: http://www.httppoint.com/member.php?userid=580
View this thread: http://www.httppoint.com/showthread.php?t=22378
Message Posted via the webmaster forum http://www.httppoint.com, (Ad revenue sharing).
Navigation:
[Reply to this message]
|