By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,780 Members | 1,550 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,780 IT Pros & Developers. It's quick & easy.

__contruct in php4 not same behavior as php5

P: n/a

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).

Nov 19 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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
\<_)
^^
Nov 19 '07 #2

P: n/a
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/
Nov 19 '07 #3

P: n/a

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).


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).

Nov 19 '07 #4

P: n/a

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).

Nov 19 '07 #5

P: n/a
On Mon, 19 Nov 2007 09:15:26 +0100, FFMG <FF*********@no-mx.httppoint.com
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 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;
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
<http://nl2.php.net/manual/en/language.references.whatdo.php>, and check
this example (PHP4 compliant):

<?php
class foo{
function foo(){
global $a;
$a =& $this;
}
}
class bar{
function bar(){
$GLOBALS['b'] =& $this;
}
}
$a = $b = null;
new foo();
var_dump($a);
new bar();
var_dump($b);
?>

As the manual states:
"Think about global $var; as a shortcut to $var =& $GLOBALS['var'];. Thus
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 = 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 constructing
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:

<?php
class bar{
public $test;
function __construct(){
echo 'constructing';
$GLOBALS['a'] = clone $this;
$this->test = 'hello';
$GLOBALS['b'] = clone $this;
}
}
$a = $b = 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
Nov 19 '07 #6

P: n/a

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).

Nov 20 '07 #7

P: n/a
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]
Nov 21 '07 #8

P: n/a

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:
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).

Nov 22 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.