Mombu the Php Forum

Go Back   Mombu the Php Forum > Php > PHP4 vs PHP5 classes
User Name
Password
REGISTER NOW! Mark Forums Read




Reply Bookmark and Share
1 21st November 10:24
External User
 
Posts: 1
Default PHP4 vs PHP5 classes



I have the following code in part of a loop:
$max = $players->max();

Important parts of players class:
class Players extends dynamicTable {
var $setup;
var $lid;
var $size;
var $max;
var $data;
var $data_result;
var $data_index;
var $player_stats_result;
var $player_stats_data;
var $player_stats_index;
var $player_stats_start;
var $player_type_result;

function Players($lid) {
global $prefix, $db;

/*
* Find max size of rows
*/

$this->lid = $lid;
$this->max=0;
echo "$this->index";
$result = $db->sql_query("select * from " . $prefix .
"_league_games_setup where lid = $lid");
while (($rows = $db->sql_fetchrow($result))) {
$c=0;
for ($i=3; $i < 23; $i++) {
if ($rows[$i] != "")
$c++;
}
if ($c > $this->max)
$this->max = $c;
}

$this->index = 3;
}


function max() {
return ($this->max);


The most important part of this code to me is:
$this->index = 3;
It is important that this is reset to 3 for the rest of the loop to work
properly.
In PHP4, $this->index is reset to 3 each time the loop happens, but in
PHP5 it is not.
It appears that after the first loop, PHP5 seems to "remember" the value
of $this->max and therefore does not enter the Players function to set
$this->index = 3. I have run through the script with a debugger, and sure
enough, we only enter function Players once.

Is this normal behaviour for PHP5 vs PHP4?
Is there a way for me to force $this->max to be calculated each time
function max is called?
  Reply With Quote


 


2 21st November 10:24
dmagick
External User
 
Posts: 1
Default PHP4 vs PHP5 classes



Since the Players method is a constructor, it's more about how you set
the object(s) up.

What does the loop look like before you create a new object?

--
Postgresql & php tutorials
http://www.designmagick.com/
  Reply With Quote
3 21st November 10:24
External User
 
Posts: 1
Default PHP4 vs PHP5 classes


Well see here is where it gets messy! This is not my code - I've ported a
phpnuke module over to dragonflycms.

The $players object is created before the loop:
$players = new Players($lid);

The loop is quite big, but here is the important part:

-------------SNIP-------------
while (list($pid, $name, $gsid, $stat) = $players->fetchSelectData()) {
if (DEBUG_LEAGUE == "YES") echo "<tr><td colspan=\"22\">\$maxcol =
$maxcol | \$pid = $pid | \$name = $name | \$gsid = $gsid | \$stat =
$stat</td></tr>\$
$noplayer=0;
if ($prevgsid != $gsid) {
$prevgsid = $gsid;
$players->selectHeaders($gsid);
if (DEBUG_LEAGUE == "YES") var_dump($players);
displayPlayerSetupRow($players,
"".getlink("$module")."&amp;&file=index&amp;mode=$ mode&amp;lid=$lid&amp;sid=$sid&amp;gsid=$gsid",
$order);
}
-------------SNIP-------------

function displayPlayerSetupRow is also very big, but here it is up to the
troublesome $players->max call:

-------------SNIP-------------
function displayPlayerSetupRow($players, $go="", $prevorder=0) {
global $bgcolor2;

if ($prevorder=="")
$prevorder=0;

$urlstart="";
$urlend="";
$max = $players->max();

-------------SNIP-------------

Then we enter class Players as previously mentioned:

-------------SNIP-------------
class Players extends dynamicTable {
var $setup;
var $lid;
var $size;
var $max;
var $data;
var $data_result;
var $data_index;
var $player_stats_result;
var $player_stats_data;
var $player_stats_index;
var $player_stats_start;
var $player_type_result;

function Players($lid) {
global $prefix, $db;

/*
* Find max size of rows
*/

$this->lid = $lid;
$this->max=0;
$result = $db->sql_query("select * from " . $prefix .
"_league_games_setup where lid = $lid");
while (($rows = $db->sql_fetchrow($result))) {
$c=0;
for ($i=3; $i < 23; $i++) {
if ($rows[$i] != "")
$c++;
}
if ($c > $this->max)
$this->max = $c;
}

$this->index = 3;
}

function name() {
return ($this->setup['name']);
}

function max() {
return ($this->max);
}
-------------SNIP-------------
  Reply With Quote
4 21st November 10:24
dmagick
External User
 
Posts: 1
Default PHP4 vs PHP5 classes


<snip>

Which means the code is only executed once since it's in the
constructor. It's not changing per loop because you're not calling the code.
Maybe setting $this->max should be done in

fetchSelectData

since that's what is causing/creating your loop.

--
Postgresql & php tutorials
http://www.designmagick.com/
  Reply With Quote
5 21st November 10:24
External User
 
Posts: 1
Default PHP4 vs PHP5 classes


Thanks Chris, I will give that a shot.
Just to confirm, this script works just fine in php4, so do we put that
down to pure luck, or has there been a change in php5 that will be causing
it?
  Reply With Quote
6 21st November 10:24
External User
 
Posts: 1
Default PHP4 vs PHP5 classes


Thanks Chris, I copied the code into the fetchSelectData function and it
seems to be working fine now!
Just need to test removing the code from the constructor to make sure its
still working, and also test that php4 is still working.

I'm just concerned that the code might also be called from somewhere else.

You've cured my headache!
  Reply With Quote
7 21st November 10:24
dmagick
External User
 
Posts: 1
Default PHP4 vs PHP5 classes


No idea why it works in php4 - if you're only calling the "Players"
method once (or through the constructor) it should have behaved the same
in php4.

--
Postgresql & php tutorials
http://www.designmagick.com/
  Reply With Quote
8 21st November 10:25
lists
External User
 
Posts: 1
Default PHP4 vs PHP5 classes


Don't forget that in PHP5, the constructor named has changed. In PHP4 it
called a method with the same name as the class. But, in PHP5, it looks for
__construct() instead.

I workaround that I use is this:

<?php

class myClass {

function __construct() {
}
function myClass() {
$this->__construct();
} }
?>

With this type of construct, your classes will initialize the same between
PHP4 and PHP5.

--
Jim Lucas

"Some men are born to greatness, some achieve greatness,
and some have greatness thrust upon them."

Twelfth Night, Act II, Scene V
by William Shakespeare
  Reply With Quote
9 21st November 10:26
dmagick
External User
 
Posts: 1
Default PHP4 vs PHP5 classes


If __construct doesn't exist then it falls back to the php4 way - makes
it backwards compatible

--
Postgresql & php tutorials
http://www.designmagick.com/
  Reply With Quote
10 21st November 10:26
lists
External User
 
Posts: 1
Default PHP4 vs PHP5 classes


But, if people want to code to php5 standards and beyond, then I would
recommend always using the __construct() method to construct your class.
Then make it work in php4. Not the other way around.
  Reply With Quote
Reply


Thread Tools
Display Modes


Some other forums that might be of your interest : Php 5 forum, Apache forum, Iis forum, Functions forum, Classes forum, Librarys forum, Bugs forum, Postgres forum, Mysql forum, Paradox forum, Ms sql forum, Configurations forum, Php.ini forum, Problems forum, Scripting forum, Css forum, General forums, Off-topic talk, Links, Extra forums, Php


Copyright © 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666