简体   繁体   中英

PHP OOP code broke php4.? to php5.6

This code was written 10-15 years ago from what I can see. Friday, the webhost stopped supporting php4 and forced upgrade to 5.6.23 and the web app stopped working. I have been going over it all weekend. Got the system running, but there is one serious issue and I've narrowed it to a specific line of code and still can't figure it out due to my not doing php OOP programming myself. Looking for a quick fix here as my friend is dead in the water.

This code:

$ins = & $_SESSION["ins"];
$serv = new Services();

print "post - ";
print_r($_POST)."<br />";
reset($_POST);
foreach ($_POST as $key => $value) {
    //$key = addslashes($key);
    $query = mysql_query("SELECT $valueNumber
                          FROM CLIENTS_SERVICES
                          where serviceID = '$key' and clientID = '$ins->client' and effectiveDate <= '$effectiveDate'
                          order by effectiveDate DESC LIMIT 1")
             or die("SELECT services function query failed!!");
    $query_data = mysql_fetch_object($query);
    $serv->value = $query_data->$valueNumber;
    $serv->quantity = $value;
    $ins->services[$key] = $serv;    //<-----  this is the offender

    // Calculate services running total
    $total = $total + ($serv->quantity * $serv->value);
    print $key." - key<br />";
    print $value." - value<br />";
    print "serv array - ";
    print_r($serv);
    print "<br />";
    print "ins array - ";
    print_r($ins->services);
    print "<br />";
    print $total." - total<br />";
}

outputs:

post - Array (
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
[6] => 1.5
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
[15] =>
[16] => 3
[17] =>
[50] =>
[51] => 1.75
[58] =>
[save_services] => Save )

6 - key
1.5 - value
serv object - Services Object ( [quantity] => 1.5 [value] => 56 )
ins array - Array ( [6] => Services Object ( [quantity] => 1.5 [value] => 56 ) )
84 - total

16 - key
3 - value
serv object - Services Object ( [quantity] => 3 [value] => 45 )
ins array - Array ( [6] => Services Object ( [quantity] => 3 [value] => 45 ) [16] => Services Object ( [quantity] => 3 [value] => 45 ) )
219 - total

51 - key
1.75 - value
serv object - Services Object ( [quantity] => 1.75 [value] => 118 )
ins array - Array ( [6] => Services Object ( [quantity] => 1.75 [value] => 118 ) [16] => Services Object ( [quantity] => 1.75 [value] => 118 ) [51] => Services Object ( [quantity] => 1.75 [value] => 118 ) )
425.5 - total

In a nutshell, I cannot understand why the most recent object values are overwriting the previous array entries. I've spent the weekend trying to figure out what changed between the old version (4.?) and the new 5.6.23, and have been on dozens of dead ends and wild goose chases. Ignorance is not bliss.

If there is a pointer to the resolution, or you can offer some insight, I'd appreciate it.

Create a new instance of services inside of the loop

foreach ($_POST as $key => $value) {
    $serv = new Services();
    ...
}

When you use objects in php it will use the same pointer to that object

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM