__contruct in php4 not same behavior as php5
__contruct in php4 not same behavior as php5
am 19.11.2007 09:15:26 von FFMG
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/
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 "
-----
\n";
$testClass->setObject();
var_dump( $globalValue );
echo "
-----
\n";
var_dump( $testClass );
echo "
-----
\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).
Re: __contruct in php4 not same behavior as php5
am 19.11.2007 11:47:10 von Toby A Inkster
FFMG wrote:
> class TestClass
> {
> function TestClass() // For php4
> {
> $this->__construct();
> }
If you really need to write code that is supported in both PHP 4 and 5,
then just use PHP-4-style constructors, as they are supported just fine
in PHP 5.
However, given PHP 4's imminent demise, I can't see much point in
supporting it much longer. Better to support 5+6 rather than 4+5.
--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.12-12mdksmp, up 12 days, 17:42.]
USD/EUR Exchange Rate Graph
http://tobyinkster.co.uk/blog/2007/11/18/usd-eur/
Re: __contruct in php4 not same behavior as php5
am 19.11.2007 11:55:37 von Ulf Kadner
FFMG wrote:
^^^^---What wrote?
> But I would like to make it backward compatible in case something bad
> happens, (and to make sure I understand what the changes are).
No. Change or let it be!
> 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()));
> }
What a stupid code! Sorry... :-)
Dont mix things like that. Separate strict PHP5 from PHP4. All other has
a bit of "Bullshit-Bingo".
I you have the real needing for PHP4 and PHP5-Version you have to do it
in seperate libraries.
Ulf
--
_,
_(_p> Ulf [Kado] Kadner
\<_)
^^
Re: __contruct in php4 not same behavior as php5
am 19.11.2007 13:02:48 von FFMG
Ulf Kadner;104243 Wrote:
>
> > But I would like to make it backward compatible in case something
> bad
> > happens, (and to make sure I understand what the changes
> are).[/color]
>
> No. Change or let it be!
>
>
Unfortunately it is not that easy.
Ulf Kadner;104243 Wrote:
>
> What a stupid code! Sorry... :-)
>
It was an example, sorry I thought it was clear enough.
I could leave the code as it is because php5 'understands' php4 style
constructors. But as I said, I just want to understand why I am not
getting the same output for what is essentially the same code.
I thought that my working example illustrated what I was asking.
Ulf Kadner;104243 Wrote:
>
> Ulf
>
^^^^---What's that?
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).
Re: __contruct in php4 not same behavior as php5
am 19.11.2007 13:17:16 von FFMG
Toby A Inkster;104244 Wrote:
>
> If you really need to write code that is supported in both PHP 4 and
> 5,
> then just use PHP-4-style constructors, as they are supported just
> fine
> in PHP 5.
>
> However, given PHP 4's imminent demise, I can't see much point in
> supporting it much longer. Better to support 5+6 rather than 4+5.
>
In this case my idea was to partly understand why the two results were
different.
As for keeping php4+php5, it is one of the steps I am doing to move to
php5.
As you might know, upgrading the code is not that easy and making the
code backward compatible does allow me to test on php4 to make sure the
output/tests are still valid and as expected.
Because I know what output is expected in php4.
I can then run the same tests on php5 knowing that the logic is still
sound.
In other words, if my tests fail on php5 I will know that it is because
of a change in behavior in php and not because I forgot some obscure
line of code somewhere.
In my 'real' code, once the php5 code is running properly and has
passed some tests I will go around and remove all the unneeded php4
code, (and then retest to make sure that I did not remove to much).
I will then upgrade to php6 but in that case support both for the
foreseeable future.
I hope that made sense.
But I am still curious as to why the output is not the same in my
original post.
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).
Re: __contruct in php4 not same behavior as php5
am 19.11.2007 18:57:38 von luiheidsgoeroe
On Mon, 19 Nov 2007 09:15:26 +0100, FFMG
m> =
wrote:
> 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 a=
m
> not getting the same behavior across the versions.
>
> // Some simple code/
>
> class TestClass
> {
> function TestClass() // For php4
> {
> $this->__construct();
> }
>
> var $_classValue =3D '';
> function __construct()// For php5 && 4
> {
> global $globalValue;
> $globalValue =3D $this;
While in PHP5 $globalValue now has a _reference_ to $this, in PHP4 =
$globalValue will have a _copy_ of $this.
There is another error: you should use the $GLOBALS array here, see =
, and check=
=
this example (PHP4 compliant):
class foo{
function foo(){
global $a;
$a =3D& $this;
}
}
class bar{
function bar(){
$GLOBALS['b'] =3D& $this;
}
}
$a =3D $b =3D null;
new foo();
var_dump($a);
new bar();
var_dump($b);
?>
As the manual states:
"Think about global $var; as a shortcut to $var =3D& $GLOBALS['var'];. T=
hus =
assigning other reference to $var only changes the local variable's =
reference."
So, as soon as you make $a in this example a reference to whatever you =
want, it no longer references $GLOBALS['a'].
> // I use a random to make certain we are talking about the same
> class.
> $this->_classValue =3D md5(uniqid(mt_rand()));
^^And this code will not be performed in you copy of $this, as it is a =
copy no constructor code will be performed, expecially this particular =
code. Never ever copy (default for PHP4) your object it your constructin=
g =
is not even done (and actually, don't create a reference (default for =
PHP5) to your object untill it is done to be sure...). Example how this =
=
would work in PHP4 translated to PHP5:
class bar{
public $test;
function __construct(){
echo 'constructing';
$GLOBALS['a'] =3D clone $this;
$this->test =3D 'hello';
$GLOBALS['b'] =3D clone $this;
}
}
$a =3D $b =3D null;
var_dump(new bar(),$a,$b);
?>
Realize:
- we have three different objects here.
- the constructor is called only once, for our first 'anonymous' object.=
- bar::test will not be set in $a as the code never gets there for that =
=
object.
-- =
Rik Wasmus
Re: __contruct in php4 not same behavior as php5
am 20.11.2007 06:23:13 von FFMG
Rik Wasmus;104287 Wrote:
>
> ...
> Rik Wasmus
Many thanks for this clear explanation, I now understand what I need to
look out for and why it was not working as I thought it should have.
I can now revisit my constructors with a better understanding.
Many 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).
Re: __contruct in php4 not same behavior as php5
am 21.11.2007 20:06:14 von oliver.graetz
FFMG schrieb:
> I am slowly moving my code to php5.
Hurry up! http://gophp5.org/
> But I would like to make it backward compatible in case something bad
> happens, (and to make sure I understand what the changes are).
>
> function TestClass() // For php4
> {
> $this->__construct();
> }
Very. Bad. Idea. Do not touch the PHP4 style constructors until you
finally stop supporting PHP4 in your application. Plan on doing this
QUICKLY! PHP4 dies! You had three and a half years time to switch to PHP5...
You will see the reference vs. copy issue explained by Rik Wasmus
everywhere in your application. PHP5 defaults to giving you references
instead of copies and the once almost-always-needed & operators left
over from your PHP4 code will in fact lead to a higher memory
consumption than necessary under PHP5 caused by the details in the
memory management of the Zend Engine but besides that your programm will
still run fine.
OLLi
--
"I hit him with a shovel." "Is he conscious?" "Yes.." "Then hit him again."
[Alias 507]
Re: __contruct in php4 not same behavior as php5
am 22.11.2007 06:49:55 von FFMG
Oliver Grätz;104701 Wrote:
> FFMG schrieb:
> > I am slowly moving my code to php5.
>
> Hurry up! http://gophp5.org/
>
Last I checked this is still my server :)
I will move to php5 when my code is ready. I should make it in time in
any case.
Oliver Grätz;104701 Wrote:
> FFMG schrieb:
> [color=blue]
> > But I would like to make it backward compatible in case something
> bad
> > happens, (and to make sure I understand what the changes are).
> >
> > function TestClass() // For php4
> > {
> > $this->__construct();
> > }
>
> Very. Bad. Idea. Do not touch the PHP4 style constructors until you
> finally stop supporting PHP4 in your application. Plan on doing this
> QUICKLY! PHP4 dies! You had three and a half years time to switch to
> PHP5...
>
> You will see the reference vs. copy issue explained by Rik Wasmus
> everywhere in your application. PHP5 defaults to giving you references
> instead of copies and the once almost-always-needed & operators left
> over from your PHP4 code will in fact lead to a higher memory
> consumption than necessary under PHP5 caused by the details in the
> memory management of the Zend Engine but besides that your programm
> will
> still run fine.
>
> OLLi
>
Well, according to
http://www.php.net/manual/en/language.oop5.decon.php,
"For backwards compatibility, if PHP 5 cannot find a __construct()
function for a given class, it will search for the old-style
constructor function, by the name of the class."
In other words, the way I read this,
> function TestClass() // For php4
> {
> $this->__construct();
> ...
will never be called in php5 because __construct() exists, so it is
truly for backward compatibility.
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).
Re: __contruct in php4 not same behavior as php5
am 22.11.2007 11:29:53 von oliver.graetz
FFMG schrieb:
> Oliver Grätz;104701 Wrote:=20
>> FFMG schrieb:
>>> I am slowly moving my code to php5.
>> Hurry up! http://gophp5.org/
>>
> Last I checked this is still my server :)
You didn't know they put self destruct time bombs into the binaries?
Just kidding. Still relying on PHP4 may still be OK after the "end of
life" but it'll potentially become dangerous to use it once any new
security holes like buffer overflow vulnerabilities are discovered.
There will be no one to fix them and thus hosting companies will start
to take PHP4 from their servers once that arises. This will be the
moment of death for applications that rely on PHP4 and need to be
deployed onto those public hosting services.
>> Do not touch the PHP4 style constructors until you
>> finally stop supporting PHP4 in your application.
>=20
> Well, according to
> http://www.php.net/manual/en/language.oop5.decon.php,=20
>=20
> "For backwards compatibility, if PHP 5 cannot find a __construct()
> function for a given class, it will search for the old-style
> constructor function, by the name of the class."
>=20
> In other words, the way I read this,=20
>=20
>> function TestClass() // For php4
>> {
>> $this->__construct();
>> ...
>=20
> will never be called in php5 because __construct() exists, so it is
> truly for backward compatibility.
Or you could say it is just for creating work out of nothing and it
bears the risk of changing one of them (other parameters) and forgetting
the other one. The "backwards compatibility" thing is already done by
PHP as the documentation clearly states and you are trying to outsmart
the language by double-solving the issue. My advice is to leave them
constructors alone until you finally switch the app to PHP5-only. And
then you don't need the backward compatibility...
OLLi
--=20
If he moves, kill him.
[Battlestar Galactica 2003, Pilot]