简体   繁体   English

PHP OOP代码打破了php4。 到php5.6

[英]PHP OOP code broke php4.? to php5.6

This code was written 10-15 years ago from what I can see. 这段代码是从我看到的10到15年前写的。 Friday, the webhost stopped supporting php4 and forced upgrade to 5.6.23 and the web app stopped working. 星期五,webhost停止支持php4并强制升级到5.6.23,网络应用程序停止工作。 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. 让系统运行,但有一个严重的问题,我已经缩小到一个特定的代码行,但仍然无法解决,因为我自己没有进行php OOP编程。 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. 我花了整个周末试图找出旧版本(4.?)和新版本5.6.23之间发生了什么变化,并且已经进行了数十次死胡同和疯狂的追逐。 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 当你在php中使用对象时,它将使用与该对象相同的指针

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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