473,915 Members | 5,904 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

__contruct in php4 not same behavior as php5


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_r and()));
}

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(TestClas s)#1 (1) { ["_classValu e"]= string(32)
"352f867d5651b6 12ea3448c7a753e 2cc" }
-----
object(TestClas s)#1 (1) { ["_classValu e"]=string(32)
"352f867d5651b6 12ea3448c7a753e 2cc" }
-----
object(TestClas s)#1 (1) { ["_classValu e"]=string(32)
"352f867d5651b6 12ea3448c7a753e 2cc" }
-----

But the output in php4 is not correct.
It is as if the class is not yet constructed.

object(testclas s)(1) { ["_classValu e"]= string(0) "" }
-----
object(testclas s)(1) { ["_classValu e"]=string(32)
"53b6f8088bd3c0 99e5b2b29e4f136 1b2" }
-----
object(testclas s)(1) { ["_classValu e"]=string(32)
"53b6f8088bd3c0 99e5b2b29e4f136 1b2" }
-----

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
8 2328
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_r and()));
}
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
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

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

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
On Mon, 19 Nov 2007 09:15:26 +0100, FFMG <FF*********@ no-mx.httppoint.co 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 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.refere nces.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_r and()));
^^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

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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2808
by: Tim Tyler | last post by:
I'm sure this is a FAQ - but I could not find a coherent statement of the answer: Some of my clients want PHP4. Other ones want PHP5. Can I run both PHP4 and PHP5 under the same instance of Apache - both on port 80 - using different file extensions to distinguish between them? --
0
2031
by: Dave Pham | last post by:
I just cleaned my comp, and I am trying to re-config my webserver... I am trying to setup apache 2 so it runs both php4 and php5, I also have two instances of mysql running. I know this can be done cos it had work before, but after this incident its not longer... These are my directories for each: Apache: D:\ServerRoot\Apache\Apache2 MySQL1: D:\ServerRoot\mysql1
1
2786
by: dk_sz | last post by:
Is it just me... Or is PHP5 XML very limited? Or am I missing something very obvious? Any way to use PHP4 Dom XML in PHP5? Does anyone know why support for it was dropped? I have following code I need to have replaced: PHP4
4
5440
by: Kevin | last post by:
Hi all, I've got a PHP4 app that I developed which I'm trying to get to run on a PHP5 server. Everything works great, except for one thing. There's a particular routine that creates an original object, then copies it. (The object constructor gets some meta information from the database, so I copy it for performance reasons). The routine then modifies the copies. PHP5 copies by reference by default, so this doesn't work--- I'm not
0
1473
by: yawnmoth | last post by:
Say I have the following script: <?php $contents="<p>test\ntest</p>"; $xml_parser = xml_parser_create('UTF-8'); // try xml_parser_create_ns, too. xml_set_character_data_handler($xml_parser,'handler'); xml_parse($xml_parser, $contents);
2
2593
by: Stefan Huber | last post by:
Hi I've got a really strange problem, and can't find out why it's not working as intended. in order to use php4 and 5 together on a webserver and the requirement for running as different users, I use suexec and a wrapper script for php files. to make it a bit clearer, i'll post the different snippets: httpd.conf:
12
2317
by: Drazen Gemic | last post by:
How long will PHP4 be supported ? When is PHP4 end of life scheduled ? DG
3
3310
by: xhe | last post by:
I have just upgraded my php version form php4 to php5. and I met this problem, and don't know if you know the solution. My site was written in PHP4, and most parts can be running smoothly in PHP5, only that in old version, I can use $row to access the data in database directly, no need to put double quote around fieldname. BUT in PHP5, this is wrong, I got error message "undefined constant". I know this is because PHP5 see the fieldname...
3
3482
by: jmark | last post by:
I am currently running php 4.4.7 in windows xp and apache 2. If I enter php in command line. I get the following error The application has failed to start because php5ts.dll was not found" I have installed both php5 and php 4.4.7 but I am not running php5 as I have all the entries in httpd.conf related to php5 commented out. I also do not have anything pointing to php5 in my environment path if I type php -i
0
10039
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11359
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
11069
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10543
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8102
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7259
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5944
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4346
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3370
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.